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