sysfs, kernfs: remove duplicated include from file.c
[platform/adaptation/renesas_rcar/renesas_kernel.git] / fs / kernfs / file.c
1 /*
2  * fs/kernfs/file.c - kernfs file implementation
3  *
4  * Copyright (c) 2001-3 Patrick Mochel
5  * Copyright (c) 2007 SUSE Linux Products GmbH
6  * Copyright (c) 2007, 2013 Tejun Heo <tj@kernel.org>
7  *
8  * This file is released under the GPLv2.
9  */
10
11 #include <linux/fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/slab.h>
14 #include <linux/poll.h>
15 #include <linux/pagemap.h>
16 #include <linux/sched.h>
17
18 #include "kernfs-internal.h"
19
20 /*
21  * There's one sysfs_open_file for each open file and one sysfs_open_dirent
22  * for each sysfs_dirent with one or more open files.
23  *
24  * sysfs_dirent->s_attr.open points to sysfs_open_dirent.  s_attr.open is
25  * protected by sysfs_open_dirent_lock.
26  *
27  * filp->private_data points to seq_file whose ->private points to
28  * sysfs_open_file.  sysfs_open_files are chained at
29  * sysfs_open_dirent->files, which is protected by sysfs_open_file_mutex.
30  */
31 static DEFINE_SPINLOCK(sysfs_open_dirent_lock);
32 static DEFINE_MUTEX(sysfs_open_file_mutex);
33
34 struct sysfs_open_dirent {
35         atomic_t                refcnt;
36         atomic_t                event;
37         wait_queue_head_t       poll;
38         struct list_head        files; /* goes through sysfs_open_file.list */
39 };
40
41 static struct sysfs_open_file *sysfs_of(struct file *file)
42 {
43         return ((struct seq_file *)file->private_data)->private;
44 }
45
46 /*
47  * Determine the kernfs_ops for the given sysfs_dirent.  This function must
48  * be called while holding an active reference.
49  */
50 static const struct kernfs_ops *kernfs_ops(struct sysfs_dirent *sd)
51 {
52         if (sd->s_flags & SYSFS_FLAG_LOCKDEP)
53                 lockdep_assert_held(sd);
54         return sd->s_attr.ops;
55 }
56
57 static void *kernfs_seq_start(struct seq_file *sf, loff_t *ppos)
58 {
59         struct sysfs_open_file *of = sf->private;
60         const struct kernfs_ops *ops;
61
62         /*
63          * @of->mutex nests outside active ref and is just to ensure that
64          * the ops aren't called concurrently for the same open file.
65          */
66         mutex_lock(&of->mutex);
67         if (!sysfs_get_active(of->sd))
68                 return ERR_PTR(-ENODEV);
69
70         ops = kernfs_ops(of->sd);
71         if (ops->seq_start) {
72                 return ops->seq_start(sf, ppos);
73         } else {
74                 /*
75                  * The same behavior and code as single_open().  Returns
76                  * !NULL if pos is at the beginning; otherwise, NULL.
77                  */
78                 return NULL + !*ppos;
79         }
80 }
81
82 static void *kernfs_seq_next(struct seq_file *sf, void *v, loff_t *ppos)
83 {
84         struct sysfs_open_file *of = sf->private;
85         const struct kernfs_ops *ops = kernfs_ops(of->sd);
86
87         if (ops->seq_next) {
88                 return ops->seq_next(sf, v, ppos);
89         } else {
90                 /*
91                  * The same behavior and code as single_open(), always
92                  * terminate after the initial read.
93                  */
94                 ++*ppos;
95                 return NULL;
96         }
97 }
98
99 static void kernfs_seq_stop(struct seq_file *sf, void *v)
100 {
101         struct sysfs_open_file *of = sf->private;
102         const struct kernfs_ops *ops = kernfs_ops(of->sd);
103
104         if (ops->seq_stop)
105                 ops->seq_stop(sf, v);
106
107         sysfs_put_active(of->sd);
108         mutex_unlock(&of->mutex);
109 }
110
111 static int kernfs_seq_show(struct seq_file *sf, void *v)
112 {
113         struct sysfs_open_file *of = sf->private;
114
115         of->event = atomic_read(&of->sd->s_attr.open->event);
116
117         return of->sd->s_attr.ops->seq_show(sf, v);
118 }
119
120 static const struct seq_operations kernfs_seq_ops = {
121         .start = kernfs_seq_start,
122         .next = kernfs_seq_next,
123         .stop = kernfs_seq_stop,
124         .show = kernfs_seq_show,
125 };
126
127 /*
128  * As reading a bin file can have side-effects, the exact offset and bytes
129  * specified in read(2) call should be passed to the read callback making
130  * it difficult to use seq_file.  Implement simplistic custom buffering for
131  * bin files.
132  */
133 static ssize_t kernfs_file_direct_read(struct sysfs_open_file *of,
134                                        char __user *user_buf, size_t count,
135                                        loff_t *ppos)
136 {
137         ssize_t len = min_t(size_t, count, PAGE_SIZE);
138         const struct kernfs_ops *ops;
139         char *buf;
140
141         buf = kmalloc(len, GFP_KERNEL);
142         if (!buf)
143                 return -ENOMEM;
144
145         /*
146          * @of->mutex nests outside active ref and is just to ensure that
147          * the ops aren't called concurrently for the same open file.
148          */
149         mutex_lock(&of->mutex);
150         if (!sysfs_get_active(of->sd)) {
151                 len = -ENODEV;
152                 mutex_unlock(&of->mutex);
153                 goto out_free;
154         }
155
156         ops = kernfs_ops(of->sd);
157         if (ops->read)
158                 len = ops->read(of, buf, len, *ppos);
159         else
160                 len = -EINVAL;
161
162         sysfs_put_active(of->sd);
163         mutex_unlock(&of->mutex);
164
165         if (len < 0)
166                 goto out_free;
167
168         if (copy_to_user(user_buf, buf, len)) {
169                 len = -EFAULT;
170                 goto out_free;
171         }
172
173         *ppos += len;
174
175  out_free:
176         kfree(buf);
177         return len;
178 }
179
180 /**
181  * kernfs_file_read - kernfs vfs read callback
182  * @file: file pointer
183  * @user_buf: data to write
184  * @count: number of bytes
185  * @ppos: starting offset
186  */
187 static ssize_t kernfs_file_read(struct file *file, char __user *user_buf,
188                                 size_t count, loff_t *ppos)
189 {
190         struct sysfs_open_file *of = sysfs_of(file);
191
192         if (of->sd->s_flags & SYSFS_FLAG_HAS_SEQ_SHOW)
193                 return seq_read(file, user_buf, count, ppos);
194         else
195                 return kernfs_file_direct_read(of, user_buf, count, ppos);
196 }
197
198 /**
199  * kernfs_file_write - kernfs vfs write callback
200  * @file: file pointer
201  * @user_buf: data to write
202  * @count: number of bytes
203  * @ppos: starting offset
204  *
205  * Copy data in from userland and pass it to the matching kernfs write
206  * operation.
207  *
208  * There is no easy way for us to know if userspace is only doing a partial
209  * write, so we don't support them. We expect the entire buffer to come on
210  * the first write.  Hint: if you're writing a value, first read the file,
211  * modify only the the value you're changing, then write entire buffer
212  * back.
213  */
214 static ssize_t kernfs_file_write(struct file *file, const char __user *user_buf,
215                                  size_t count, loff_t *ppos)
216 {
217         struct sysfs_open_file *of = sysfs_of(file);
218         ssize_t len = min_t(size_t, count, PAGE_SIZE);
219         const struct kernfs_ops *ops;
220         char *buf;
221
222         buf = kmalloc(len + 1, GFP_KERNEL);
223         if (!buf)
224                 return -ENOMEM;
225
226         if (copy_from_user(buf, user_buf, len)) {
227                 len = -EFAULT;
228                 goto out_free;
229         }
230         buf[len] = '\0';        /* guarantee string termination */
231
232         /*
233          * @of->mutex nests outside active ref and is just to ensure that
234          * the ops aren't called concurrently for the same open file.
235          */
236         mutex_lock(&of->mutex);
237         if (!sysfs_get_active(of->sd)) {
238                 mutex_unlock(&of->mutex);
239                 len = -ENODEV;
240                 goto out_free;
241         }
242
243         ops = kernfs_ops(of->sd);
244         if (ops->write)
245                 len = ops->write(of, buf, len, *ppos);
246         else
247                 len = -EINVAL;
248
249         sysfs_put_active(of->sd);
250         mutex_unlock(&of->mutex);
251
252         if (len > 0)
253                 *ppos += len;
254 out_free:
255         kfree(buf);
256         return len;
257 }
258
259 static void kernfs_vma_open(struct vm_area_struct *vma)
260 {
261         struct file *file = vma->vm_file;
262         struct sysfs_open_file *of = sysfs_of(file);
263
264         if (!of->vm_ops)
265                 return;
266
267         if (!sysfs_get_active(of->sd))
268                 return;
269
270         if (of->vm_ops->open)
271                 of->vm_ops->open(vma);
272
273         sysfs_put_active(of->sd);
274 }
275
276 static int kernfs_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
277 {
278         struct file *file = vma->vm_file;
279         struct sysfs_open_file *of = sysfs_of(file);
280         int ret;
281
282         if (!of->vm_ops)
283                 return VM_FAULT_SIGBUS;
284
285         if (!sysfs_get_active(of->sd))
286                 return VM_FAULT_SIGBUS;
287
288         ret = VM_FAULT_SIGBUS;
289         if (of->vm_ops->fault)
290                 ret = of->vm_ops->fault(vma, vmf);
291
292         sysfs_put_active(of->sd);
293         return ret;
294 }
295
296 static int kernfs_vma_page_mkwrite(struct vm_area_struct *vma,
297                                    struct vm_fault *vmf)
298 {
299         struct file *file = vma->vm_file;
300         struct sysfs_open_file *of = sysfs_of(file);
301         int ret;
302
303         if (!of->vm_ops)
304                 return VM_FAULT_SIGBUS;
305
306         if (!sysfs_get_active(of->sd))
307                 return VM_FAULT_SIGBUS;
308
309         ret = 0;
310         if (of->vm_ops->page_mkwrite)
311                 ret = of->vm_ops->page_mkwrite(vma, vmf);
312         else
313                 file_update_time(file);
314
315         sysfs_put_active(of->sd);
316         return ret;
317 }
318
319 static int kernfs_vma_access(struct vm_area_struct *vma, unsigned long addr,
320                              void *buf, int len, int write)
321 {
322         struct file *file = vma->vm_file;
323         struct sysfs_open_file *of = sysfs_of(file);
324         int ret;
325
326         if (!of->vm_ops)
327                 return -EINVAL;
328
329         if (!sysfs_get_active(of->sd))
330                 return -EINVAL;
331
332         ret = -EINVAL;
333         if (of->vm_ops->access)
334                 ret = of->vm_ops->access(vma, addr, buf, len, write);
335
336         sysfs_put_active(of->sd);
337         return ret;
338 }
339
340 #ifdef CONFIG_NUMA
341 static int kernfs_vma_set_policy(struct vm_area_struct *vma,
342                                  struct mempolicy *new)
343 {
344         struct file *file = vma->vm_file;
345         struct sysfs_open_file *of = sysfs_of(file);
346         int ret;
347
348         if (!of->vm_ops)
349                 return 0;
350
351         if (!sysfs_get_active(of->sd))
352                 return -EINVAL;
353
354         ret = 0;
355         if (of->vm_ops->set_policy)
356                 ret = of->vm_ops->set_policy(vma, new);
357
358         sysfs_put_active(of->sd);
359         return ret;
360 }
361
362 static struct mempolicy *kernfs_vma_get_policy(struct vm_area_struct *vma,
363                                                unsigned long addr)
364 {
365         struct file *file = vma->vm_file;
366         struct sysfs_open_file *of = sysfs_of(file);
367         struct mempolicy *pol;
368
369         if (!of->vm_ops)
370                 return vma->vm_policy;
371
372         if (!sysfs_get_active(of->sd))
373                 return vma->vm_policy;
374
375         pol = vma->vm_policy;
376         if (of->vm_ops->get_policy)
377                 pol = of->vm_ops->get_policy(vma, addr);
378
379         sysfs_put_active(of->sd);
380         return pol;
381 }
382
383 static int kernfs_vma_migrate(struct vm_area_struct *vma,
384                               const nodemask_t *from, const nodemask_t *to,
385                               unsigned long flags)
386 {
387         struct file *file = vma->vm_file;
388         struct sysfs_open_file *of = sysfs_of(file);
389         int ret;
390
391         if (!of->vm_ops)
392                 return 0;
393
394         if (!sysfs_get_active(of->sd))
395                 return 0;
396
397         ret = 0;
398         if (of->vm_ops->migrate)
399                 ret = of->vm_ops->migrate(vma, from, to, flags);
400
401         sysfs_put_active(of->sd);
402         return ret;
403 }
404 #endif
405
406 static const struct vm_operations_struct kernfs_vm_ops = {
407         .open           = kernfs_vma_open,
408         .fault          = kernfs_vma_fault,
409         .page_mkwrite   = kernfs_vma_page_mkwrite,
410         .access         = kernfs_vma_access,
411 #ifdef CONFIG_NUMA
412         .set_policy     = kernfs_vma_set_policy,
413         .get_policy     = kernfs_vma_get_policy,
414         .migrate        = kernfs_vma_migrate,
415 #endif
416 };
417
418 static int kernfs_file_mmap(struct file *file, struct vm_area_struct *vma)
419 {
420         struct sysfs_open_file *of = sysfs_of(file);
421         const struct kernfs_ops *ops;
422         int rc;
423
424         mutex_lock(&of->mutex);
425
426         rc = -ENODEV;
427         if (!sysfs_get_active(of->sd))
428                 goto out_unlock;
429
430         ops = kernfs_ops(of->sd);
431         if (ops->mmap)
432                 rc = ops->mmap(of, vma);
433         if (rc)
434                 goto out_put;
435
436         /*
437          * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
438          * to satisfy versions of X which crash if the mmap fails: that
439          * substitutes a new vm_file, and we don't then want bin_vm_ops.
440          */
441         if (vma->vm_file != file)
442                 goto out_put;
443
444         rc = -EINVAL;
445         if (of->mmapped && of->vm_ops != vma->vm_ops)
446                 goto out_put;
447
448         /*
449          * It is not possible to successfully wrap close.
450          * So error if someone is trying to use close.
451          */
452         rc = -EINVAL;
453         if (vma->vm_ops && vma->vm_ops->close)
454                 goto out_put;
455
456         rc = 0;
457         of->mmapped = 1;
458         of->vm_ops = vma->vm_ops;
459         vma->vm_ops = &kernfs_vm_ops;
460 out_put:
461         sysfs_put_active(of->sd);
462 out_unlock:
463         mutex_unlock(&of->mutex);
464
465         return rc;
466 }
467
468 /**
469  *      sysfs_get_open_dirent - get or create sysfs_open_dirent
470  *      @sd: target sysfs_dirent
471  *      @of: sysfs_open_file for this instance of open
472  *
473  *      If @sd->s_attr.open exists, increment its reference count;
474  *      otherwise, create one.  @of is chained to the files list.
475  *
476  *      LOCKING:
477  *      Kernel thread context (may sleep).
478  *
479  *      RETURNS:
480  *      0 on success, -errno on failure.
481  */
482 static int sysfs_get_open_dirent(struct sysfs_dirent *sd,
483                                  struct sysfs_open_file *of)
484 {
485         struct sysfs_open_dirent *od, *new_od = NULL;
486
487  retry:
488         mutex_lock(&sysfs_open_file_mutex);
489         spin_lock_irq(&sysfs_open_dirent_lock);
490
491         if (!sd->s_attr.open && new_od) {
492                 sd->s_attr.open = new_od;
493                 new_od = NULL;
494         }
495
496         od = sd->s_attr.open;
497         if (od) {
498                 atomic_inc(&od->refcnt);
499                 list_add_tail(&of->list, &od->files);
500         }
501
502         spin_unlock_irq(&sysfs_open_dirent_lock);
503         mutex_unlock(&sysfs_open_file_mutex);
504
505         if (od) {
506                 kfree(new_od);
507                 return 0;
508         }
509
510         /* not there, initialize a new one and retry */
511         new_od = kmalloc(sizeof(*new_od), GFP_KERNEL);
512         if (!new_od)
513                 return -ENOMEM;
514
515         atomic_set(&new_od->refcnt, 0);
516         atomic_set(&new_od->event, 1);
517         init_waitqueue_head(&new_od->poll);
518         INIT_LIST_HEAD(&new_od->files);
519         goto retry;
520 }
521
522 /**
523  *      sysfs_put_open_dirent - put sysfs_open_dirent
524  *      @sd: target sysfs_dirent
525  *      @of: associated sysfs_open_file
526  *
527  *      Put @sd->s_attr.open and unlink @of from the files list.  If
528  *      reference count reaches zero, disassociate and free it.
529  *
530  *      LOCKING:
531  *      None.
532  */
533 static void sysfs_put_open_dirent(struct sysfs_dirent *sd,
534                                   struct sysfs_open_file *of)
535 {
536         struct sysfs_open_dirent *od = sd->s_attr.open;
537         unsigned long flags;
538
539         mutex_lock(&sysfs_open_file_mutex);
540         spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
541
542         if (of)
543                 list_del(&of->list);
544
545         if (atomic_dec_and_test(&od->refcnt))
546                 sd->s_attr.open = NULL;
547         else
548                 od = NULL;
549
550         spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
551         mutex_unlock(&sysfs_open_file_mutex);
552
553         kfree(od);
554 }
555
556 static int kernfs_file_open(struct inode *inode, struct file *file)
557 {
558         struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
559         const struct kernfs_ops *ops;
560         struct sysfs_open_file *of;
561         bool has_read, has_write, has_mmap;
562         int error = -EACCES;
563
564         if (!sysfs_get_active(attr_sd))
565                 return -ENODEV;
566
567         ops = kernfs_ops(attr_sd);
568
569         has_read = ops->seq_show || ops->read || ops->mmap;
570         has_write = ops->write || ops->mmap;
571         has_mmap = ops->mmap;
572
573         /* check perms and supported operations */
574         if ((file->f_mode & FMODE_WRITE) &&
575             (!(inode->i_mode & S_IWUGO) || !has_write))
576                 goto err_out;
577
578         if ((file->f_mode & FMODE_READ) &&
579             (!(inode->i_mode & S_IRUGO) || !has_read))
580                 goto err_out;
581
582         /* allocate a sysfs_open_file for the file */
583         error = -ENOMEM;
584         of = kzalloc(sizeof(struct sysfs_open_file), GFP_KERNEL);
585         if (!of)
586                 goto err_out;
587
588         /*
589          * The following is done to give a different lockdep key to
590          * @of->mutex for files which implement mmap.  This is a rather
591          * crude way to avoid false positive lockdep warning around
592          * mm->mmap_sem - mmap nests @of->mutex under mm->mmap_sem and
593          * reading /sys/block/sda/trace/act_mask grabs sr_mutex, under
594          * which mm->mmap_sem nests, while holding @of->mutex.  As each
595          * open file has a separate mutex, it's okay as long as those don't
596          * happen on the same file.  At this point, we can't easily give
597          * each file a separate locking class.  Let's differentiate on
598          * whether the file has mmap or not for now.
599          */
600         if (has_mmap)
601                 mutex_init(&of->mutex);
602         else
603                 mutex_init(&of->mutex);
604
605         of->sd = attr_sd;
606         of->file = file;
607
608         /*
609          * Always instantiate seq_file even if read access doesn't use
610          * seq_file or is not requested.  This unifies private data access
611          * and readable regular files are the vast majority anyway.
612          */
613         if (ops->seq_show)
614                 error = seq_open(file, &kernfs_seq_ops);
615         else
616                 error = seq_open(file, NULL);
617         if (error)
618                 goto err_free;
619
620         ((struct seq_file *)file->private_data)->private = of;
621
622         /* seq_file clears PWRITE unconditionally, restore it if WRITE */
623         if (file->f_mode & FMODE_WRITE)
624                 file->f_mode |= FMODE_PWRITE;
625
626         /* make sure we have open dirent struct */
627         error = sysfs_get_open_dirent(attr_sd, of);
628         if (error)
629                 goto err_close;
630
631         /* open succeeded, put active references */
632         sysfs_put_active(attr_sd);
633         return 0;
634
635 err_close:
636         seq_release(inode, file);
637 err_free:
638         kfree(of);
639 err_out:
640         sysfs_put_active(attr_sd);
641         return error;
642 }
643
644 static int kernfs_file_release(struct inode *inode, struct file *filp)
645 {
646         struct sysfs_dirent *sd = filp->f_path.dentry->d_fsdata;
647         struct sysfs_open_file *of = sysfs_of(filp);
648
649         sysfs_put_open_dirent(sd, of);
650         seq_release(inode, filp);
651         kfree(of);
652
653         return 0;
654 }
655
656 void sysfs_unmap_bin_file(struct sysfs_dirent *sd)
657 {
658         struct sysfs_open_dirent *od;
659         struct sysfs_open_file *of;
660
661         if (!(sd->s_flags & SYSFS_FLAG_HAS_MMAP))
662                 return;
663
664         spin_lock_irq(&sysfs_open_dirent_lock);
665         od = sd->s_attr.open;
666         if (od)
667                 atomic_inc(&od->refcnt);
668         spin_unlock_irq(&sysfs_open_dirent_lock);
669         if (!od)
670                 return;
671
672         mutex_lock(&sysfs_open_file_mutex);
673         list_for_each_entry(of, &od->files, list) {
674                 struct inode *inode = file_inode(of->file);
675                 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
676         }
677         mutex_unlock(&sysfs_open_file_mutex);
678
679         sysfs_put_open_dirent(sd, NULL);
680 }
681
682 /* Sysfs attribute files are pollable.  The idea is that you read
683  * the content and then you use 'poll' or 'select' to wait for
684  * the content to change.  When the content changes (assuming the
685  * manager for the kobject supports notification), poll will
686  * return POLLERR|POLLPRI, and select will return the fd whether
687  * it is waiting for read, write, or exceptions.
688  * Once poll/select indicates that the value has changed, you
689  * need to close and re-open the file, or seek to 0 and read again.
690  * Reminder: this only works for attributes which actively support
691  * it, and it is not possible to test an attribute from userspace
692  * to see if it supports poll (Neither 'poll' nor 'select' return
693  * an appropriate error code).  When in doubt, set a suitable timeout value.
694  */
695 static unsigned int kernfs_file_poll(struct file *filp, poll_table *wait)
696 {
697         struct sysfs_open_file *of = sysfs_of(filp);
698         struct sysfs_dirent *attr_sd = filp->f_path.dentry->d_fsdata;
699         struct sysfs_open_dirent *od = attr_sd->s_attr.open;
700
701         /* need parent for the kobj, grab both */
702         if (!sysfs_get_active(attr_sd))
703                 goto trigger;
704
705         poll_wait(filp, &od->poll, wait);
706
707         sysfs_put_active(attr_sd);
708
709         if (of->event != atomic_read(&od->event))
710                 goto trigger;
711
712         return DEFAULT_POLLMASK;
713
714  trigger:
715         return DEFAULT_POLLMASK|POLLERR|POLLPRI;
716 }
717
718 /**
719  * kernfs_notify - notify a kernfs file
720  * @sd: file to notify
721  *
722  * Notify @sd such that poll(2) on @sd wakes up.
723  */
724 void kernfs_notify(struct sysfs_dirent *sd)
725 {
726         struct sysfs_open_dirent *od;
727         unsigned long flags;
728
729         spin_lock_irqsave(&sysfs_open_dirent_lock, flags);
730
731         if (!WARN_ON(sysfs_type(sd) != SYSFS_KOBJ_ATTR)) {
732                 od = sd->s_attr.open;
733                 if (od) {
734                         atomic_inc(&od->event);
735                         wake_up_interruptible(&od->poll);
736                 }
737         }
738
739         spin_unlock_irqrestore(&sysfs_open_dirent_lock, flags);
740 }
741 EXPORT_SYMBOL_GPL(kernfs_notify);
742
743 const struct file_operations kernfs_file_operations = {
744         .read           = kernfs_file_read,
745         .write          = kernfs_file_write,
746         .llseek         = generic_file_llseek,
747         .mmap           = kernfs_file_mmap,
748         .open           = kernfs_file_open,
749         .release        = kernfs_file_release,
750         .poll           = kernfs_file_poll,
751 };
752
753 /**
754  * kernfs_create_file_ns_key - create a file
755  * @parent: directory to create the file in
756  * @name: name of the file
757  * @mode: mode of the file
758  * @size: size of the file
759  * @ops: kernfs operations for the file
760  * @priv: private data for the file
761  * @ns: optional namespace tag of the file
762  * @key: lockdep key for the file's active_ref, %NULL to disable lockdep
763  *
764  * Returns the created node on success, ERR_PTR() value on error.
765  */
766 struct sysfs_dirent *kernfs_create_file_ns_key(struct sysfs_dirent *parent,
767                                                const char *name,
768                                                umode_t mode, loff_t size,
769                                                const struct kernfs_ops *ops,
770                                                void *priv, const void *ns,
771                                                struct lock_class_key *key)
772 {
773         struct sysfs_addrm_cxt acxt;
774         struct sysfs_dirent *sd;
775         int rc;
776
777         sd = sysfs_new_dirent(kernfs_root(parent), name,
778                               (mode & S_IALLUGO) | S_IFREG, SYSFS_KOBJ_ATTR);
779         if (!sd)
780                 return ERR_PTR(-ENOMEM);
781
782         sd->s_attr.ops = ops;
783         sd->s_attr.size = size;
784         sd->s_ns = ns;
785         sd->priv = priv;
786
787 #ifdef CONFIG_DEBUG_LOCK_ALLOC
788         if (key) {
789                 lockdep_init_map(&sd->dep_map, "s_active", key, 0);
790                 sd->s_flags |= SYSFS_FLAG_LOCKDEP;
791         }
792 #endif
793
794         /*
795          * sd->s_attr.ops is accesible only while holding active ref.  We
796          * need to know whether some ops are implemented outside active
797          * ref.  Cache their existence in flags.
798          */
799         if (ops->seq_show)
800                 sd->s_flags |= SYSFS_FLAG_HAS_SEQ_SHOW;
801         if (ops->mmap)
802                 sd->s_flags |= SYSFS_FLAG_HAS_MMAP;
803
804         sysfs_addrm_start(&acxt);
805         rc = sysfs_add_one(&acxt, sd, parent);
806         sysfs_addrm_finish(&acxt);
807
808         if (rc) {
809                 kernfs_put(sd);
810                 return ERR_PTR(rc);
811         }
812         return sd;
813 }