Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/fs/file.c
0003  *
0004  *  Copyright (C) 1998-1999, Stephen Tweedie and Bill Hawes
0005  *
0006  *  Manage the dynamic fd arrays in the process files_struct.
0007  */
0008 
0009 #include <linux/syscalls.h>
0010 #include <linux/export.h>
0011 #include <linux/fs.h>
0012 #include <linux/mm.h>
0013 #include <linux/mmzone.h>
0014 #include <linux/time.h>
0015 #include <linux/sched.h>
0016 #include <linux/slab.h>
0017 #include <linux/vmalloc.h>
0018 #include <linux/file.h>
0019 #include <linux/fdtable.h>
0020 #include <linux/bitops.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/spinlock.h>
0023 #include <linux/rcupdate.h>
0024 #include <linux/workqueue.h>
0025 
0026 unsigned int sysctl_nr_open __read_mostly = 1024*1024;
0027 unsigned int sysctl_nr_open_min = BITS_PER_LONG;
0028 /* our min() is unusable in constant expressions ;-/ */
0029 #define __const_min(x, y) ((x) < (y) ? (x) : (y))
0030 unsigned int sysctl_nr_open_max =
0031     __const_min(INT_MAX, ~(size_t)0/sizeof(void *)) & -BITS_PER_LONG;
0032 
0033 static void *alloc_fdmem(size_t size)
0034 {
0035     /*
0036      * Very large allocations can stress page reclaim, so fall back to
0037      * vmalloc() if the allocation size will be considered "large" by the VM.
0038      */
0039     if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
0040         void *data = kmalloc(size, GFP_KERNEL_ACCOUNT |
0041                      __GFP_NOWARN | __GFP_NORETRY);
0042         if (data != NULL)
0043             return data;
0044     }
0045     return __vmalloc(size, GFP_KERNEL_ACCOUNT | __GFP_HIGHMEM, PAGE_KERNEL);
0046 }
0047 
0048 static void __free_fdtable(struct fdtable *fdt)
0049 {
0050     kvfree(fdt->fd);
0051     kvfree(fdt->open_fds);
0052     kfree(fdt);
0053 }
0054 
0055 static void free_fdtable_rcu(struct rcu_head *rcu)
0056 {
0057     __free_fdtable(container_of(rcu, struct fdtable, rcu));
0058 }
0059 
0060 #define BITBIT_NR(nr)   BITS_TO_LONGS(BITS_TO_LONGS(nr))
0061 #define BITBIT_SIZE(nr) (BITBIT_NR(nr) * sizeof(long))
0062 
0063 /*
0064  * Copy 'count' fd bits from the old table to the new table and clear the extra
0065  * space if any.  This does not copy the file pointers.  Called with the files
0066  * spinlock held for write.
0067  */
0068 static void copy_fd_bitmaps(struct fdtable *nfdt, struct fdtable *ofdt,
0069                 unsigned int count)
0070 {
0071     unsigned int cpy, set;
0072 
0073     cpy = count / BITS_PER_BYTE;
0074     set = (nfdt->max_fds - count) / BITS_PER_BYTE;
0075     memcpy(nfdt->open_fds, ofdt->open_fds, cpy);
0076     memset((char *)nfdt->open_fds + cpy, 0, set);
0077     memcpy(nfdt->close_on_exec, ofdt->close_on_exec, cpy);
0078     memset((char *)nfdt->close_on_exec + cpy, 0, set);
0079 
0080     cpy = BITBIT_SIZE(count);
0081     set = BITBIT_SIZE(nfdt->max_fds) - cpy;
0082     memcpy(nfdt->full_fds_bits, ofdt->full_fds_bits, cpy);
0083     memset((char *)nfdt->full_fds_bits + cpy, 0, set);
0084 }
0085 
0086 /*
0087  * Copy all file descriptors from the old table to the new, expanded table and
0088  * clear the extra space.  Called with the files spinlock held for write.
0089  */
0090 static void copy_fdtable(struct fdtable *nfdt, struct fdtable *ofdt)
0091 {
0092     unsigned int cpy, set;
0093 
0094     BUG_ON(nfdt->max_fds < ofdt->max_fds);
0095 
0096     cpy = ofdt->max_fds * sizeof(struct file *);
0097     set = (nfdt->max_fds - ofdt->max_fds) * sizeof(struct file *);
0098     memcpy(nfdt->fd, ofdt->fd, cpy);
0099     memset((char *)nfdt->fd + cpy, 0, set);
0100 
0101     copy_fd_bitmaps(nfdt, ofdt, ofdt->max_fds);
0102 }
0103 
0104 static struct fdtable * alloc_fdtable(unsigned int nr)
0105 {
0106     struct fdtable *fdt;
0107     void *data;
0108 
0109     /*
0110      * Figure out how many fds we actually want to support in this fdtable.
0111      * Allocation steps are keyed to the size of the fdarray, since it
0112      * grows far faster than any of the other dynamic data. We try to fit
0113      * the fdarray into comfortable page-tuned chunks: starting at 1024B
0114      * and growing in powers of two from there on.
0115      */
0116     nr /= (1024 / sizeof(struct file *));
0117     nr = roundup_pow_of_two(nr + 1);
0118     nr *= (1024 / sizeof(struct file *));
0119     /*
0120      * Note that this can drive nr *below* what we had passed if sysctl_nr_open
0121      * had been set lower between the check in expand_files() and here.  Deal
0122      * with that in caller, it's cheaper that way.
0123      *
0124      * We make sure that nr remains a multiple of BITS_PER_LONG - otherwise
0125      * bitmaps handling below becomes unpleasant, to put it mildly...
0126      */
0127     if (unlikely(nr > sysctl_nr_open))
0128         nr = ((sysctl_nr_open - 1) | (BITS_PER_LONG - 1)) + 1;
0129 
0130     fdt = kmalloc(sizeof(struct fdtable), GFP_KERNEL_ACCOUNT);
0131     if (!fdt)
0132         goto out;
0133     fdt->max_fds = nr;
0134     data = alloc_fdmem(nr * sizeof(struct file *));
0135     if (!data)
0136         goto out_fdt;
0137     fdt->fd = data;
0138 
0139     data = alloc_fdmem(max_t(size_t,
0140                  2 * nr / BITS_PER_BYTE + BITBIT_SIZE(nr), L1_CACHE_BYTES));
0141     if (!data)
0142         goto out_arr;
0143     fdt->open_fds = data;
0144     data += nr / BITS_PER_BYTE;
0145     fdt->close_on_exec = data;
0146     data += nr / BITS_PER_BYTE;
0147     fdt->full_fds_bits = data;
0148 
0149     return fdt;
0150 
0151 out_arr:
0152     kvfree(fdt->fd);
0153 out_fdt:
0154     kfree(fdt);
0155 out:
0156     return NULL;
0157 }
0158 
0159 /*
0160  * Expand the file descriptor table.
0161  * This function will allocate a new fdtable and both fd array and fdset, of
0162  * the given size.
0163  * Return <0 error code on error; 1 on successful completion.
0164  * The files->file_lock should be held on entry, and will be held on exit.
0165  */
0166 static int expand_fdtable(struct files_struct *files, unsigned int nr)
0167     __releases(files->file_lock)
0168     __acquires(files->file_lock)
0169 {
0170     struct fdtable *new_fdt, *cur_fdt;
0171 
0172     spin_unlock(&files->file_lock);
0173     new_fdt = alloc_fdtable(nr);
0174 
0175     /* make sure all __fd_install() have seen resize_in_progress
0176      * or have finished their rcu_read_lock_sched() section.
0177      */
0178     if (atomic_read(&files->count) > 1)
0179         synchronize_sched();
0180 
0181     spin_lock(&files->file_lock);
0182     if (!new_fdt)
0183         return -ENOMEM;
0184     /*
0185      * extremely unlikely race - sysctl_nr_open decreased between the check in
0186      * caller and alloc_fdtable().  Cheaper to catch it here...
0187      */
0188     if (unlikely(new_fdt->max_fds <= nr)) {
0189         __free_fdtable(new_fdt);
0190         return -EMFILE;
0191     }
0192     cur_fdt = files_fdtable(files);
0193     BUG_ON(nr < cur_fdt->max_fds);
0194     copy_fdtable(new_fdt, cur_fdt);
0195     rcu_assign_pointer(files->fdt, new_fdt);
0196     if (cur_fdt != &files->fdtab)
0197         call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
0198     /* coupled with smp_rmb() in __fd_install() */
0199     smp_wmb();
0200     return 1;
0201 }
0202 
0203 /*
0204  * Expand files.
0205  * This function will expand the file structures, if the requested size exceeds
0206  * the current capacity and there is room for expansion.
0207  * Return <0 error code on error; 0 when nothing done; 1 when files were
0208  * expanded and execution may have blocked.
0209  * The files->file_lock should be held on entry, and will be held on exit.
0210  */
0211 static int expand_files(struct files_struct *files, unsigned int nr)
0212     __releases(files->file_lock)
0213     __acquires(files->file_lock)
0214 {
0215     struct fdtable *fdt;
0216     int expanded = 0;
0217 
0218 repeat:
0219     fdt = files_fdtable(files);
0220 
0221     /* Do we need to expand? */
0222     if (nr < fdt->max_fds)
0223         return expanded;
0224 
0225     /* Can we expand? */
0226     if (nr >= sysctl_nr_open)
0227         return -EMFILE;
0228 
0229     if (unlikely(files->resize_in_progress)) {
0230         spin_unlock(&files->file_lock);
0231         expanded = 1;
0232         wait_event(files->resize_wait, !files->resize_in_progress);
0233         spin_lock(&files->file_lock);
0234         goto repeat;
0235     }
0236 
0237     /* All good, so we try */
0238     files->resize_in_progress = true;
0239     expanded = expand_fdtable(files, nr);
0240     files->resize_in_progress = false;
0241 
0242     wake_up_all(&files->resize_wait);
0243     return expanded;
0244 }
0245 
0246 static inline void __set_close_on_exec(unsigned int fd, struct fdtable *fdt)
0247 {
0248     __set_bit(fd, fdt->close_on_exec);
0249 }
0250 
0251 static inline void __clear_close_on_exec(unsigned int fd, struct fdtable *fdt)
0252 {
0253     if (test_bit(fd, fdt->close_on_exec))
0254         __clear_bit(fd, fdt->close_on_exec);
0255 }
0256 
0257 static inline void __set_open_fd(unsigned int fd, struct fdtable *fdt)
0258 {
0259     __set_bit(fd, fdt->open_fds);
0260     fd /= BITS_PER_LONG;
0261     if (!~fdt->open_fds[fd])
0262         __set_bit(fd, fdt->full_fds_bits);
0263 }
0264 
0265 static inline void __clear_open_fd(unsigned int fd, struct fdtable *fdt)
0266 {
0267     __clear_bit(fd, fdt->open_fds);
0268     __clear_bit(fd / BITS_PER_LONG, fdt->full_fds_bits);
0269 }
0270 
0271 static unsigned int count_open_files(struct fdtable *fdt)
0272 {
0273     unsigned int size = fdt->max_fds;
0274     unsigned int i;
0275 
0276     /* Find the last open fd */
0277     for (i = size / BITS_PER_LONG; i > 0; ) {
0278         if (fdt->open_fds[--i])
0279             break;
0280     }
0281     i = (i + 1) * BITS_PER_LONG;
0282     return i;
0283 }
0284 
0285 /*
0286  * Allocate a new files structure and copy contents from the
0287  * passed in files structure.
0288  * errorp will be valid only when the returned files_struct is NULL.
0289  */
0290 struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
0291 {
0292     struct files_struct *newf;
0293     struct file **old_fds, **new_fds;
0294     unsigned int open_files, i;
0295     struct fdtable *old_fdt, *new_fdt;
0296 
0297     *errorp = -ENOMEM;
0298     newf = kmem_cache_alloc(files_cachep, GFP_KERNEL);
0299     if (!newf)
0300         goto out;
0301 
0302     atomic_set(&newf->count, 1);
0303 
0304     spin_lock_init(&newf->file_lock);
0305     newf->resize_in_progress = false;
0306     init_waitqueue_head(&newf->resize_wait);
0307     newf->next_fd = 0;
0308     new_fdt = &newf->fdtab;
0309     new_fdt->max_fds = NR_OPEN_DEFAULT;
0310     new_fdt->close_on_exec = newf->close_on_exec_init;
0311     new_fdt->open_fds = newf->open_fds_init;
0312     new_fdt->full_fds_bits = newf->full_fds_bits_init;
0313     new_fdt->fd = &newf->fd_array[0];
0314 
0315     spin_lock(&oldf->file_lock);
0316     old_fdt = files_fdtable(oldf);
0317     open_files = count_open_files(old_fdt);
0318 
0319     /*
0320      * Check whether we need to allocate a larger fd array and fd set.
0321      */
0322     while (unlikely(open_files > new_fdt->max_fds)) {
0323         spin_unlock(&oldf->file_lock);
0324 
0325         if (new_fdt != &newf->fdtab)
0326             __free_fdtable(new_fdt);
0327 
0328         new_fdt = alloc_fdtable(open_files - 1);
0329         if (!new_fdt) {
0330             *errorp = -ENOMEM;
0331             goto out_release;
0332         }
0333 
0334         /* beyond sysctl_nr_open; nothing to do */
0335         if (unlikely(new_fdt->max_fds < open_files)) {
0336             __free_fdtable(new_fdt);
0337             *errorp = -EMFILE;
0338             goto out_release;
0339         }
0340 
0341         /*
0342          * Reacquire the oldf lock and a pointer to its fd table
0343          * who knows it may have a new bigger fd table. We need
0344          * the latest pointer.
0345          */
0346         spin_lock(&oldf->file_lock);
0347         old_fdt = files_fdtable(oldf);
0348         open_files = count_open_files(old_fdt);
0349     }
0350 
0351     copy_fd_bitmaps(new_fdt, old_fdt, open_files);
0352 
0353     old_fds = old_fdt->fd;
0354     new_fds = new_fdt->fd;
0355 
0356     for (i = open_files; i != 0; i--) {
0357         struct file *f = *old_fds++;
0358         if (f) {
0359             get_file(f);
0360         } else {
0361             /*
0362              * The fd may be claimed in the fd bitmap but not yet
0363              * instantiated in the files array if a sibling thread
0364              * is partway through open().  So make sure that this
0365              * fd is available to the new process.
0366              */
0367             __clear_open_fd(open_files - i, new_fdt);
0368         }
0369         rcu_assign_pointer(*new_fds++, f);
0370     }
0371     spin_unlock(&oldf->file_lock);
0372 
0373     /* clear the remainder */
0374     memset(new_fds, 0, (new_fdt->max_fds - open_files) * sizeof(struct file *));
0375 
0376     rcu_assign_pointer(newf->fdt, new_fdt);
0377 
0378     return newf;
0379 
0380 out_release:
0381     kmem_cache_free(files_cachep, newf);
0382 out:
0383     return NULL;
0384 }
0385 
0386 static struct fdtable *close_files(struct files_struct * files)
0387 {
0388     /*
0389      * It is safe to dereference the fd table without RCU or
0390      * ->file_lock because this is the last reference to the
0391      * files structure.
0392      */
0393     struct fdtable *fdt = rcu_dereference_raw(files->fdt);
0394     unsigned int i, j = 0;
0395 
0396     for (;;) {
0397         unsigned long set;
0398         i = j * BITS_PER_LONG;
0399         if (i >= fdt->max_fds)
0400             break;
0401         set = fdt->open_fds[j++];
0402         while (set) {
0403             if (set & 1) {
0404                 struct file * file = xchg(&fdt->fd[i], NULL);
0405                 if (file) {
0406                     filp_close(file, files);
0407                     cond_resched_rcu_qs();
0408                 }
0409             }
0410             i++;
0411             set >>= 1;
0412         }
0413     }
0414 
0415     return fdt;
0416 }
0417 
0418 struct files_struct *get_files_struct(struct task_struct *task)
0419 {
0420     struct files_struct *files;
0421 
0422     task_lock(task);
0423     files = task->files;
0424     if (files)
0425         atomic_inc(&files->count);
0426     task_unlock(task);
0427 
0428     return files;
0429 }
0430 
0431 void put_files_struct(struct files_struct *files)
0432 {
0433     if (atomic_dec_and_test(&files->count)) {
0434         struct fdtable *fdt = close_files(files);
0435 
0436         /* free the arrays if they are not embedded */
0437         if (fdt != &files->fdtab)
0438             __free_fdtable(fdt);
0439         kmem_cache_free(files_cachep, files);
0440     }
0441 }
0442 
0443 void reset_files_struct(struct files_struct *files)
0444 {
0445     struct task_struct *tsk = current;
0446     struct files_struct *old;
0447 
0448     old = tsk->files;
0449     task_lock(tsk);
0450     tsk->files = files;
0451     task_unlock(tsk);
0452     put_files_struct(old);
0453 }
0454 
0455 void exit_files(struct task_struct *tsk)
0456 {
0457     struct files_struct * files = tsk->files;
0458 
0459     if (files) {
0460         task_lock(tsk);
0461         tsk->files = NULL;
0462         task_unlock(tsk);
0463         put_files_struct(files);
0464     }
0465 }
0466 
0467 struct files_struct init_files = {
0468     .count      = ATOMIC_INIT(1),
0469     .fdt        = &init_files.fdtab,
0470     .fdtab      = {
0471         .max_fds    = NR_OPEN_DEFAULT,
0472         .fd     = &init_files.fd_array[0],
0473         .close_on_exec  = init_files.close_on_exec_init,
0474         .open_fds   = init_files.open_fds_init,
0475         .full_fds_bits  = init_files.full_fds_bits_init,
0476     },
0477     .file_lock  = __SPIN_LOCK_UNLOCKED(init_files.file_lock),
0478 };
0479 
0480 static unsigned int find_next_fd(struct fdtable *fdt, unsigned int start)
0481 {
0482     unsigned int maxfd = fdt->max_fds;
0483     unsigned int maxbit = maxfd / BITS_PER_LONG;
0484     unsigned int bitbit = start / BITS_PER_LONG;
0485 
0486     bitbit = find_next_zero_bit(fdt->full_fds_bits, maxbit, bitbit) * BITS_PER_LONG;
0487     if (bitbit > maxfd)
0488         return maxfd;
0489     if (bitbit > start)
0490         start = bitbit;
0491     return find_next_zero_bit(fdt->open_fds, maxfd, start);
0492 }
0493 
0494 /*
0495  * allocate a file descriptor, mark it busy.
0496  */
0497 int __alloc_fd(struct files_struct *files,
0498            unsigned start, unsigned end, unsigned flags)
0499 {
0500     unsigned int fd;
0501     int error;
0502     struct fdtable *fdt;
0503 
0504     spin_lock(&files->file_lock);
0505 repeat:
0506     fdt = files_fdtable(files);
0507     fd = start;
0508     if (fd < files->next_fd)
0509         fd = files->next_fd;
0510 
0511     if (fd < fdt->max_fds)
0512         fd = find_next_fd(fdt, fd);
0513 
0514     /*
0515      * N.B. For clone tasks sharing a files structure, this test
0516      * will limit the total number of files that can be opened.
0517      */
0518     error = -EMFILE;
0519     if (fd >= end)
0520         goto out;
0521 
0522     error = expand_files(files, fd);
0523     if (error < 0)
0524         goto out;
0525 
0526     /*
0527      * If we needed to expand the fs array we
0528      * might have blocked - try again.
0529      */
0530     if (error)
0531         goto repeat;
0532 
0533     if (start <= files->next_fd)
0534         files->next_fd = fd + 1;
0535 
0536     __set_open_fd(fd, fdt);
0537     if (flags & O_CLOEXEC)
0538         __set_close_on_exec(fd, fdt);
0539     else
0540         __clear_close_on_exec(fd, fdt);
0541     error = fd;
0542 #if 1
0543     /* Sanity check */
0544     if (rcu_access_pointer(fdt->fd[fd]) != NULL) {
0545         printk(KERN_WARNING "alloc_fd: slot %d not NULL!\n", fd);
0546         rcu_assign_pointer(fdt->fd[fd], NULL);
0547     }
0548 #endif
0549 
0550 out:
0551     spin_unlock(&files->file_lock);
0552     return error;
0553 }
0554 
0555 static int alloc_fd(unsigned start, unsigned flags)
0556 {
0557     return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
0558 }
0559 
0560 int get_unused_fd_flags(unsigned flags)
0561 {
0562     return __alloc_fd(current->files, 0, rlimit(RLIMIT_NOFILE), flags);
0563 }
0564 EXPORT_SYMBOL(get_unused_fd_flags);
0565 
0566 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
0567 {
0568     struct fdtable *fdt = files_fdtable(files);
0569     __clear_open_fd(fd, fdt);
0570     if (fd < files->next_fd)
0571         files->next_fd = fd;
0572 }
0573 
0574 void put_unused_fd(unsigned int fd)
0575 {
0576     struct files_struct *files = current->files;
0577     spin_lock(&files->file_lock);
0578     __put_unused_fd(files, fd);
0579     spin_unlock(&files->file_lock);
0580 }
0581 
0582 EXPORT_SYMBOL(put_unused_fd);
0583 
0584 /*
0585  * Install a file pointer in the fd array.
0586  *
0587  * The VFS is full of places where we drop the files lock between
0588  * setting the open_fds bitmap and installing the file in the file
0589  * array.  At any such point, we are vulnerable to a dup2() race
0590  * installing a file in the array before us.  We need to detect this and
0591  * fput() the struct file we are about to overwrite in this case.
0592  *
0593  * It should never happen - if we allow dup2() do it, _really_ bad things
0594  * will follow.
0595  *
0596  * NOTE: __fd_install() variant is really, really low-level; don't
0597  * use it unless you are forced to by truly lousy API shoved down
0598  * your throat.  'files' *MUST* be either current->files or obtained
0599  * by get_files_struct(current) done by whoever had given it to you,
0600  * or really bad things will happen.  Normally you want to use
0601  * fd_install() instead.
0602  */
0603 
0604 void __fd_install(struct files_struct *files, unsigned int fd,
0605         struct file *file)
0606 {
0607     struct fdtable *fdt;
0608 
0609     might_sleep();
0610     rcu_read_lock_sched();
0611 
0612     while (unlikely(files->resize_in_progress)) {
0613         rcu_read_unlock_sched();
0614         wait_event(files->resize_wait, !files->resize_in_progress);
0615         rcu_read_lock_sched();
0616     }
0617     /* coupled with smp_wmb() in expand_fdtable() */
0618     smp_rmb();
0619     fdt = rcu_dereference_sched(files->fdt);
0620     BUG_ON(fdt->fd[fd] != NULL);
0621     rcu_assign_pointer(fdt->fd[fd], file);
0622     rcu_read_unlock_sched();
0623 }
0624 
0625 void fd_install(unsigned int fd, struct file *file)
0626 {
0627     __fd_install(current->files, fd, file);
0628 }
0629 
0630 EXPORT_SYMBOL(fd_install);
0631 
0632 /*
0633  * The same warnings as for __alloc_fd()/__fd_install() apply here...
0634  */
0635 int __close_fd(struct files_struct *files, unsigned fd)
0636 {
0637     struct file *file;
0638     struct fdtable *fdt;
0639 
0640     spin_lock(&files->file_lock);
0641     fdt = files_fdtable(files);
0642     if (fd >= fdt->max_fds)
0643         goto out_unlock;
0644     file = fdt->fd[fd];
0645     if (!file)
0646         goto out_unlock;
0647     rcu_assign_pointer(fdt->fd[fd], NULL);
0648     __clear_close_on_exec(fd, fdt);
0649     __put_unused_fd(files, fd);
0650     spin_unlock(&files->file_lock);
0651     return filp_close(file, files);
0652 
0653 out_unlock:
0654     spin_unlock(&files->file_lock);
0655     return -EBADF;
0656 }
0657 
0658 void do_close_on_exec(struct files_struct *files)
0659 {
0660     unsigned i;
0661     struct fdtable *fdt;
0662 
0663     /* exec unshares first */
0664     spin_lock(&files->file_lock);
0665     for (i = 0; ; i++) {
0666         unsigned long set;
0667         unsigned fd = i * BITS_PER_LONG;
0668         fdt = files_fdtable(files);
0669         if (fd >= fdt->max_fds)
0670             break;
0671         set = fdt->close_on_exec[i];
0672         if (!set)
0673             continue;
0674         fdt->close_on_exec[i] = 0;
0675         for ( ; set ; fd++, set >>= 1) {
0676             struct file *file;
0677             if (!(set & 1))
0678                 continue;
0679             file = fdt->fd[fd];
0680             if (!file)
0681                 continue;
0682             rcu_assign_pointer(fdt->fd[fd], NULL);
0683             __put_unused_fd(files, fd);
0684             spin_unlock(&files->file_lock);
0685             filp_close(file, files);
0686             cond_resched();
0687             spin_lock(&files->file_lock);
0688         }
0689 
0690     }
0691     spin_unlock(&files->file_lock);
0692 }
0693 
0694 static struct file *__fget(unsigned int fd, fmode_t mask)
0695 {
0696     struct files_struct *files = current->files;
0697     struct file *file;
0698 
0699     rcu_read_lock();
0700 loop:
0701     file = fcheck_files(files, fd);
0702     if (file) {
0703         /* File object ref couldn't be taken.
0704          * dup2() atomicity guarantee is the reason
0705          * we loop to catch the new file (or NULL pointer)
0706          */
0707         if (file->f_mode & mask)
0708             file = NULL;
0709         else if (!get_file_rcu(file))
0710             goto loop;
0711     }
0712     rcu_read_unlock();
0713 
0714     return file;
0715 }
0716 
0717 struct file *fget(unsigned int fd)
0718 {
0719     return __fget(fd, FMODE_PATH);
0720 }
0721 EXPORT_SYMBOL(fget);
0722 
0723 struct file *fget_raw(unsigned int fd)
0724 {
0725     return __fget(fd, 0);
0726 }
0727 EXPORT_SYMBOL(fget_raw);
0728 
0729 /*
0730  * Lightweight file lookup - no refcnt increment if fd table isn't shared.
0731  *
0732  * You can use this instead of fget if you satisfy all of the following
0733  * conditions:
0734  * 1) You must call fput_light before exiting the syscall and returning control
0735  *    to userspace (i.e. you cannot remember the returned struct file * after
0736  *    returning to userspace).
0737  * 2) You must not call filp_close on the returned struct file * in between
0738  *    calls to fget_light and fput_light.
0739  * 3) You must not clone the current task in between the calls to fget_light
0740  *    and fput_light.
0741  *
0742  * The fput_needed flag returned by fget_light should be passed to the
0743  * corresponding fput_light.
0744  */
0745 static unsigned long __fget_light(unsigned int fd, fmode_t mask)
0746 {
0747     struct files_struct *files = current->files;
0748     struct file *file;
0749 
0750     if (atomic_read(&files->count) == 1) {
0751         file = __fcheck_files(files, fd);
0752         if (!file || unlikely(file->f_mode & mask))
0753             return 0;
0754         return (unsigned long)file;
0755     } else {
0756         file = __fget(fd, mask);
0757         if (!file)
0758             return 0;
0759         return FDPUT_FPUT | (unsigned long)file;
0760     }
0761 }
0762 unsigned long __fdget(unsigned int fd)
0763 {
0764     return __fget_light(fd, FMODE_PATH);
0765 }
0766 EXPORT_SYMBOL(__fdget);
0767 
0768 unsigned long __fdget_raw(unsigned int fd)
0769 {
0770     return __fget_light(fd, 0);
0771 }
0772 
0773 unsigned long __fdget_pos(unsigned int fd)
0774 {
0775     unsigned long v = __fdget(fd);
0776     struct file *file = (struct file *)(v & ~3);
0777 
0778     if (file && (file->f_mode & FMODE_ATOMIC_POS)) {
0779         if (file_count(file) > 1) {
0780             v |= FDPUT_POS_UNLOCK;
0781             mutex_lock(&file->f_pos_lock);
0782         }
0783     }
0784     return v;
0785 }
0786 
0787 void __f_unlock_pos(struct file *f)
0788 {
0789     mutex_unlock(&f->f_pos_lock);
0790 }
0791 
0792 /*
0793  * We only lock f_pos if we have threads or if the file might be
0794  * shared with another process. In both cases we'll have an elevated
0795  * file count (done either by fdget() or by fork()).
0796  */
0797 
0798 void set_close_on_exec(unsigned int fd, int flag)
0799 {
0800     struct files_struct *files = current->files;
0801     struct fdtable *fdt;
0802     spin_lock(&files->file_lock);
0803     fdt = files_fdtable(files);
0804     if (flag)
0805         __set_close_on_exec(fd, fdt);
0806     else
0807         __clear_close_on_exec(fd, fdt);
0808     spin_unlock(&files->file_lock);
0809 }
0810 
0811 bool get_close_on_exec(unsigned int fd)
0812 {
0813     struct files_struct *files = current->files;
0814     struct fdtable *fdt;
0815     bool res;
0816     rcu_read_lock();
0817     fdt = files_fdtable(files);
0818     res = close_on_exec(fd, fdt);
0819     rcu_read_unlock();
0820     return res;
0821 }
0822 
0823 static int do_dup2(struct files_struct *files,
0824     struct file *file, unsigned fd, unsigned flags)
0825 __releases(&files->file_lock)
0826 {
0827     struct file *tofree;
0828     struct fdtable *fdt;
0829 
0830     /*
0831      * We need to detect attempts to do dup2() over allocated but still
0832      * not finished descriptor.  NB: OpenBSD avoids that at the price of
0833      * extra work in their equivalent of fget() - they insert struct
0834      * file immediately after grabbing descriptor, mark it larval if
0835      * more work (e.g. actual opening) is needed and make sure that
0836      * fget() treats larval files as absent.  Potentially interesting,
0837      * but while extra work in fget() is trivial, locking implications
0838      * and amount of surgery on open()-related paths in VFS are not.
0839      * FreeBSD fails with -EBADF in the same situation, NetBSD "solution"
0840      * deadlocks in rather amusing ways, AFAICS.  All of that is out of
0841      * scope of POSIX or SUS, since neither considers shared descriptor
0842      * tables and this condition does not arise without those.
0843      */
0844     fdt = files_fdtable(files);
0845     tofree = fdt->fd[fd];
0846     if (!tofree && fd_is_open(fd, fdt))
0847         goto Ebusy;
0848     get_file(file);
0849     rcu_assign_pointer(fdt->fd[fd], file);
0850     __set_open_fd(fd, fdt);
0851     if (flags & O_CLOEXEC)
0852         __set_close_on_exec(fd, fdt);
0853     else
0854         __clear_close_on_exec(fd, fdt);
0855     spin_unlock(&files->file_lock);
0856 
0857     if (tofree)
0858         filp_close(tofree, files);
0859 
0860     return fd;
0861 
0862 Ebusy:
0863     spin_unlock(&files->file_lock);
0864     return -EBUSY;
0865 }
0866 
0867 int replace_fd(unsigned fd, struct file *file, unsigned flags)
0868 {
0869     int err;
0870     struct files_struct *files = current->files;
0871 
0872     if (!file)
0873         return __close_fd(files, fd);
0874 
0875     if (fd >= rlimit(RLIMIT_NOFILE))
0876         return -EBADF;
0877 
0878     spin_lock(&files->file_lock);
0879     err = expand_files(files, fd);
0880     if (unlikely(err < 0))
0881         goto out_unlock;
0882     return do_dup2(files, file, fd, flags);
0883 
0884 out_unlock:
0885     spin_unlock(&files->file_lock);
0886     return err;
0887 }
0888 
0889 SYSCALL_DEFINE3(dup3, unsigned int, oldfd, unsigned int, newfd, int, flags)
0890 {
0891     int err = -EBADF;
0892     struct file *file;
0893     struct files_struct *files = current->files;
0894 
0895     if ((flags & ~O_CLOEXEC) != 0)
0896         return -EINVAL;
0897 
0898     if (unlikely(oldfd == newfd))
0899         return -EINVAL;
0900 
0901     if (newfd >= rlimit(RLIMIT_NOFILE))
0902         return -EBADF;
0903 
0904     spin_lock(&files->file_lock);
0905     err = expand_files(files, newfd);
0906     file = fcheck(oldfd);
0907     if (unlikely(!file))
0908         goto Ebadf;
0909     if (unlikely(err < 0)) {
0910         if (err == -EMFILE)
0911             goto Ebadf;
0912         goto out_unlock;
0913     }
0914     return do_dup2(files, file, newfd, flags);
0915 
0916 Ebadf:
0917     err = -EBADF;
0918 out_unlock:
0919     spin_unlock(&files->file_lock);
0920     return err;
0921 }
0922 
0923 SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
0924 {
0925     if (unlikely(newfd == oldfd)) { /* corner case */
0926         struct files_struct *files = current->files;
0927         int retval = oldfd;
0928 
0929         rcu_read_lock();
0930         if (!fcheck_files(files, oldfd))
0931             retval = -EBADF;
0932         rcu_read_unlock();
0933         return retval;
0934     }
0935     return sys_dup3(oldfd, newfd, 0);
0936 }
0937 
0938 SYSCALL_DEFINE1(dup, unsigned int, fildes)
0939 {
0940     int ret = -EBADF;
0941     struct file *file = fget_raw(fildes);
0942 
0943     if (file) {
0944         ret = get_unused_fd_flags(0);
0945         if (ret >= 0)
0946             fd_install(ret, file);
0947         else
0948             fput(file);
0949     }
0950     return ret;
0951 }
0952 
0953 int f_dupfd(unsigned int from, struct file *file, unsigned flags)
0954 {
0955     int err;
0956     if (from >= rlimit(RLIMIT_NOFILE))
0957         return -EINVAL;
0958     err = alloc_fd(from, flags);
0959     if (err >= 0) {
0960         get_file(file);
0961         fd_install(err, file);
0962     }
0963     return err;
0964 }
0965 
0966 int iterate_fd(struct files_struct *files, unsigned n,
0967         int (*f)(const void *, struct file *, unsigned),
0968         const void *p)
0969 {
0970     struct fdtable *fdt;
0971     int res = 0;
0972     if (!files)
0973         return 0;
0974     spin_lock(&files->file_lock);
0975     for (fdt = files_fdtable(files); n < fdt->max_fds; n++) {
0976         struct file *file;
0977         file = rcu_dereference_check_fdtable(files, fdt->fd[n]);
0978         if (!file)
0979             continue;
0980         res = f(p, file, n);
0981         if (res)
0982             break;
0983     }
0984     spin_unlock(&files->file_lock);
0985     return res;
0986 }
0987 EXPORT_SYMBOL(iterate_fd);