···367367Why: Should be implemented in userspace, policy daemon.368368Who: Johannes Berg <johannes@sipsolutions.net>369369370370----------------------------371371-372372-What: CONFIG_INOTIFY373373-When: 2.6.33374374-Why: last user (audit) will be converted to the newer more generic375375- and more easily maintained fsnotify subsystem376376-Who: Eric Paris <eparis@redhat.com>377377-378370----------------------------379371380372What: lock_policy_rwsem_* and unlock_policy_rwsem_* will not be
···11-config INOTIFY22- bool "Inotify file change notification support"33- default n44- ---help---55- Say Y here to enable legacy in kernel inotify support. Inotify is a66- file change notification system. It is a replacement for dnotify.77- This option only provides the legacy inotify in kernel API. There88- are no in tree kernel users of this interface since it is deprecated.99- You only need this if you are loading an out of tree kernel module1010- that uses inotify.1111-1212- For more information, see <file:Documentation/filesystems/inotify.txt>1313-1414- If unsure, say N.1515-161config INOTIFY_USER172 bool "Inotify support for userspace"183 select ANON_INODES
···11-/*22- * fs/inotify.c - inode-based file event notifications33- *44- * Authors:55- * John McCutchan <ttb@tentacle.dhs.org>66- * Robert Love <rml@novell.com>77- *88- * Kernel API added by: Amy Griffis <amy.griffis@hp.com>99- *1010- * Copyright (C) 2005 John McCutchan1111- * Copyright 2006 Hewlett-Packard Development Company, L.P.1212- *1313- * This program is free software; you can redistribute it and/or modify it1414- * under the terms of the GNU General Public License as published by the1515- * Free Software Foundation; either version 2, or (at your option) any1616- * later version.1717- *1818- * This program is distributed in the hope that it will be useful, but1919- * WITHOUT ANY WARRANTY; without even the implied warranty of2020- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU2121- * General Public License for more details.2222- */2323-2424-#include <linux/module.h>2525-#include <linux/kernel.h>2626-#include <linux/spinlock.h>2727-#include <linux/idr.h>2828-#include <linux/slab.h>2929-#include <linux/fs.h>3030-#include <linux/sched.h>3131-#include <linux/init.h>3232-#include <linux/list.h>3333-#include <linux/writeback.h>3434-#include <linux/inotify.h>3535-#include <linux/fsnotify_backend.h>3636-3737-static atomic_t inotify_cookie;3838-3939-/*4040- * Lock ordering:4141- *4242- * dentry->d_lock (used to keep d_move() away from dentry->d_parent)4343- * iprune_mutex (synchronize shrink_icache_memory())4444- * inode_lock (protects the super_block->s_inodes list)4545- * inode->inotify_mutex (protects inode->inotify_watches and watches->i_list)4646- * inotify_handle->mutex (protects inotify_handle and watches->h_list)4747- *4848- * The inode->inotify_mutex and inotify_handle->mutex and held during execution4949- * of a caller's event handler. Thus, the caller must not hold any locks5050- * taken in their event handler while calling any of the published inotify5151- * interfaces.5252- */5353-5454-/*5555- * Lifetimes of the three main data structures--inotify_handle, inode, and5656- * inotify_watch--are managed by reference count.5757- *5858- * inotify_handle: Lifetime is from inotify_init() to inotify_destroy().5959- * Additional references can bump the count via get_inotify_handle() and drop6060- * the count via put_inotify_handle().6161- *6262- * inotify_watch: for inotify's purposes, lifetime is from inotify_add_watch()6363- * to remove_watch_no_event(). Additional references can bump the count via6464- * get_inotify_watch() and drop the count via put_inotify_watch(). The caller6565- * is reponsible for the final put after receiving IN_IGNORED, or when using6666- * IN_ONESHOT after receiving the first event. Inotify does the final put if6767- * inotify_destroy() is called.6868- *6969- * inode: Pinned so long as the inode is associated with a watch, from7070- * inotify_add_watch() to the final put_inotify_watch().7171- */7272-7373-/*7474- * struct inotify_handle - represents an inotify instance7575- *7676- * This structure is protected by the mutex 'mutex'.7777- */7878-struct inotify_handle {7979- struct idr idr; /* idr mapping wd -> watch */8080- struct mutex mutex; /* protects this bad boy */8181- struct list_head watches; /* list of watches */8282- atomic_t count; /* reference count */8383- u32 last_wd; /* the last wd allocated */8484- const struct inotify_operations *in_ops; /* inotify caller operations */8585-};8686-8787-static inline void get_inotify_handle(struct inotify_handle *ih)8888-{8989- atomic_inc(&ih->count);9090-}9191-9292-static inline void put_inotify_handle(struct inotify_handle *ih)9393-{9494- if (atomic_dec_and_test(&ih->count)) {9595- idr_destroy(&ih->idr);9696- kfree(ih);9797- }9898-}9999-100100-/**101101- * get_inotify_watch - grab a reference to an inotify_watch102102- * @watch: watch to grab103103- */104104-void get_inotify_watch(struct inotify_watch *watch)105105-{106106- atomic_inc(&watch->count);107107-}108108-EXPORT_SYMBOL_GPL(get_inotify_watch);109109-110110-int pin_inotify_watch(struct inotify_watch *watch)111111-{112112- struct super_block *sb = watch->inode->i_sb;113113- if (atomic_inc_not_zero(&sb->s_active)) {114114- atomic_inc(&watch->count);115115- return 1;116116- }117117- return 0;118118-}119119-120120-/**121121- * put_inotify_watch - decrements the ref count on a given watch. cleans up122122- * watch references if the count reaches zero. inotify_watch is freed by123123- * inotify callers via the destroy_watch() op.124124- * @watch: watch to release125125- */126126-void put_inotify_watch(struct inotify_watch *watch)127127-{128128- if (atomic_dec_and_test(&watch->count)) {129129- struct inotify_handle *ih = watch->ih;130130-131131- iput(watch->inode);132132- ih->in_ops->destroy_watch(watch);133133- put_inotify_handle(ih);134134- }135135-}136136-EXPORT_SYMBOL_GPL(put_inotify_watch);137137-138138-void unpin_inotify_watch(struct inotify_watch *watch)139139-{140140- struct super_block *sb = watch->inode->i_sb;141141- put_inotify_watch(watch);142142- deactivate_super(sb);143143-}144144-145145-/*146146- * inotify_handle_get_wd - returns the next WD for use by the given handle147147- *148148- * Callers must hold ih->mutex. This function can sleep.149149- */150150-static int inotify_handle_get_wd(struct inotify_handle *ih,151151- struct inotify_watch *watch)152152-{153153- int ret;154154-155155- do {156156- if (unlikely(!idr_pre_get(&ih->idr, GFP_NOFS)))157157- return -ENOSPC;158158- ret = idr_get_new_above(&ih->idr, watch, ih->last_wd+1, &watch->wd);159159- } while (ret == -EAGAIN);160160-161161- if (likely(!ret))162162- ih->last_wd = watch->wd;163163-164164- return ret;165165-}166166-167167-/*168168- * inotify_inode_watched - returns nonzero if there are watches on this inode169169- * and zero otherwise. We call this lockless, we do not care if we race.170170- */171171-static inline int inotify_inode_watched(struct inode *inode)172172-{173173- return !list_empty(&inode->inotify_watches);174174-}175175-176176-/*177177- * Get child dentry flag into synch with parent inode.178178- * Flag should always be clear for negative dentrys.179179- */180180-static void set_dentry_child_flags(struct inode *inode, int watched)181181-{182182- struct dentry *alias;183183-184184- spin_lock(&dcache_lock);185185- list_for_each_entry(alias, &inode->i_dentry, d_alias) {186186- struct dentry *child;187187-188188- list_for_each_entry(child, &alias->d_subdirs, d_u.d_child) {189189- if (!child->d_inode)190190- continue;191191-192192- spin_lock(&child->d_lock);193193- if (watched)194194- child->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;195195- else196196- child->d_flags &=~DCACHE_INOTIFY_PARENT_WATCHED;197197- spin_unlock(&child->d_lock);198198- }199199- }200200- spin_unlock(&dcache_lock);201201-}202202-203203-/*204204- * inotify_find_handle - find the watch associated with the given inode and205205- * handle206206- *207207- * Callers must hold inode->inotify_mutex.208208- */209209-static struct inotify_watch *inode_find_handle(struct inode *inode,210210- struct inotify_handle *ih)211211-{212212- struct inotify_watch *watch;213213-214214- list_for_each_entry(watch, &inode->inotify_watches, i_list) {215215- if (watch->ih == ih)216216- return watch;217217- }218218-219219- return NULL;220220-}221221-222222-/*223223- * remove_watch_no_event - remove watch without the IN_IGNORED event.224224- *225225- * Callers must hold both inode->inotify_mutex and ih->mutex.226226- */227227-static void remove_watch_no_event(struct inotify_watch *watch,228228- struct inotify_handle *ih)229229-{230230- list_del(&watch->i_list);231231- list_del(&watch->h_list);232232-233233- if (!inotify_inode_watched(watch->inode))234234- set_dentry_child_flags(watch->inode, 0);235235-236236- idr_remove(&ih->idr, watch->wd);237237-}238238-239239-/**240240- * inotify_remove_watch_locked - Remove a watch from both the handle and the241241- * inode. Sends the IN_IGNORED event signifying that the inode is no longer242242- * watched. May be invoked from a caller's event handler.243243- * @ih: inotify handle associated with watch244244- * @watch: watch to remove245245- *246246- * Callers must hold both inode->inotify_mutex and ih->mutex.247247- */248248-void inotify_remove_watch_locked(struct inotify_handle *ih,249249- struct inotify_watch *watch)250250-{251251- remove_watch_no_event(watch, ih);252252- ih->in_ops->handle_event(watch, watch->wd, IN_IGNORED, 0, NULL, NULL);253253-}254254-EXPORT_SYMBOL_GPL(inotify_remove_watch_locked);255255-256256-/* Kernel API for producing events */257257-258258-/*259259- * inotify_d_instantiate - instantiate dcache entry for inode260260- */261261-void inotify_d_instantiate(struct dentry *entry, struct inode *inode)262262-{263263- struct dentry *parent;264264-265265- if (!inode)266266- return;267267-268268- spin_lock(&entry->d_lock);269269- parent = entry->d_parent;270270- if (parent->d_inode && inotify_inode_watched(parent->d_inode))271271- entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;272272- spin_unlock(&entry->d_lock);273273-}274274-275275-/*276276- * inotify_d_move - dcache entry has been moved277277- */278278-void inotify_d_move(struct dentry *entry)279279-{280280- struct dentry *parent;281281-282282- parent = entry->d_parent;283283- if (inotify_inode_watched(parent->d_inode))284284- entry->d_flags |= DCACHE_INOTIFY_PARENT_WATCHED;285285- else286286- entry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;287287-}288288-289289-/**290290- * inotify_inode_queue_event - queue an event to all watches on this inode291291- * @inode: inode event is originating from292292- * @mask: event mask describing this event293293- * @cookie: cookie for synchronization, or zero294294- * @name: filename, if any295295- * @n_inode: inode associated with name296296- */297297-void inotify_inode_queue_event(struct inode *inode, u32 mask, u32 cookie,298298- const char *name, struct inode *n_inode)299299-{300300- struct inotify_watch *watch, *next;301301-302302- if (!inotify_inode_watched(inode))303303- return;304304-305305- mutex_lock(&inode->inotify_mutex);306306- list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {307307- u32 watch_mask = watch->mask;308308- if (watch_mask & mask) {309309- struct inotify_handle *ih= watch->ih;310310- mutex_lock(&ih->mutex);311311- if (watch_mask & IN_ONESHOT)312312- remove_watch_no_event(watch, ih);313313- ih->in_ops->handle_event(watch, watch->wd, mask, cookie,314314- name, n_inode);315315- mutex_unlock(&ih->mutex);316316- }317317- }318318- mutex_unlock(&inode->inotify_mutex);319319-}320320-EXPORT_SYMBOL_GPL(inotify_inode_queue_event);321321-322322-/**323323- * inotify_dentry_parent_queue_event - queue an event to a dentry's parent324324- * @dentry: the dentry in question, we queue against this dentry's parent325325- * @mask: event mask describing this event326326- * @cookie: cookie for synchronization, or zero327327- * @name: filename, if any328328- */329329-void inotify_dentry_parent_queue_event(struct dentry *dentry, u32 mask,330330- u32 cookie, const char *name)331331-{332332- struct dentry *parent;333333- struct inode *inode;334334-335335- if (!(dentry->d_flags & DCACHE_INOTIFY_PARENT_WATCHED))336336- return;337337-338338- spin_lock(&dentry->d_lock);339339- parent = dentry->d_parent;340340- inode = parent->d_inode;341341-342342- if (inotify_inode_watched(inode)) {343343- dget(parent);344344- spin_unlock(&dentry->d_lock);345345- inotify_inode_queue_event(inode, mask, cookie, name,346346- dentry->d_inode);347347- dput(parent);348348- } else349349- spin_unlock(&dentry->d_lock);350350-}351351-EXPORT_SYMBOL_GPL(inotify_dentry_parent_queue_event);352352-353353-/**354354- * inotify_get_cookie - return a unique cookie for use in synchronizing events.355355- */356356-u32 inotify_get_cookie(void)357357-{358358- return atomic_inc_return(&inotify_cookie);359359-}360360-EXPORT_SYMBOL_GPL(inotify_get_cookie);361361-362362-/**363363- * inotify_unmount_inodes - an sb is unmounting. handle any watched inodes.364364- * @list: list of inodes being unmounted (sb->s_inodes)365365- *366366- * Called with inode_lock held, protecting the unmounting super block's list367367- * of inodes, and with iprune_mutex held, keeping shrink_icache_memory() at bay.368368- * We temporarily drop inode_lock, however, and CAN block.369369- */370370-void inotify_unmount_inodes(struct list_head *list)371371-{372372- struct inode *inode, *next_i, *need_iput = NULL;373373-374374- list_for_each_entry_safe(inode, next_i, list, i_sb_list) {375375- struct inotify_watch *watch, *next_w;376376- struct inode *need_iput_tmp;377377- struct list_head *watches;378378-379379- /*380380- * We cannot __iget() an inode in state I_CLEAR, I_FREEING,381381- * I_WILL_FREE, or I_NEW which is fine because by that point382382- * the inode cannot have any associated watches.383383- */384384- if (inode->i_state & (I_CLEAR|I_FREEING|I_WILL_FREE|I_NEW))385385- continue;386386-387387- /*388388- * If i_count is zero, the inode cannot have any watches and389389- * doing an __iget/iput with MS_ACTIVE clear would actually390390- * evict all inodes with zero i_count from icache which is391391- * unnecessarily violent and may in fact be illegal to do.392392- */393393- if (!atomic_read(&inode->i_count))394394- continue;395395-396396- need_iput_tmp = need_iput;397397- need_iput = NULL;398398- /* In case inotify_remove_watch_locked() drops a reference. */399399- if (inode != need_iput_tmp)400400- __iget(inode);401401- else402402- need_iput_tmp = NULL;403403- /* In case the dropping of a reference would nuke next_i. */404404- if ((&next_i->i_sb_list != list) &&405405- atomic_read(&next_i->i_count) &&406406- !(next_i->i_state & (I_CLEAR | I_FREEING |407407- I_WILL_FREE))) {408408- __iget(next_i);409409- need_iput = next_i;410410- }411411-412412- /*413413- * We can safely drop inode_lock here because we hold414414- * references on both inode and next_i. Also no new inodes415415- * will be added since the umount has begun. Finally,416416- * iprune_mutex keeps shrink_icache_memory() away.417417- */418418- spin_unlock(&inode_lock);419419-420420- if (need_iput_tmp)421421- iput(need_iput_tmp);422422-423423- /* for each watch, send IN_UNMOUNT and then remove it */424424- mutex_lock(&inode->inotify_mutex);425425- watches = &inode->inotify_watches;426426- list_for_each_entry_safe(watch, next_w, watches, i_list) {427427- struct inotify_handle *ih= watch->ih;428428- get_inotify_watch(watch);429429- mutex_lock(&ih->mutex);430430- ih->in_ops->handle_event(watch, watch->wd, IN_UNMOUNT, 0,431431- NULL, NULL);432432- inotify_remove_watch_locked(ih, watch);433433- mutex_unlock(&ih->mutex);434434- put_inotify_watch(watch);435435- }436436- mutex_unlock(&inode->inotify_mutex);437437- iput(inode); 438438-439439- spin_lock(&inode_lock);440440- }441441-}442442-EXPORT_SYMBOL_GPL(inotify_unmount_inodes);443443-444444-/**445445- * inotify_inode_is_dead - an inode has been deleted, cleanup any watches446446- * @inode: inode that is about to be removed447447- */448448-void inotify_inode_is_dead(struct inode *inode)449449-{450450- struct inotify_watch *watch, *next;451451-452452- mutex_lock(&inode->inotify_mutex);453453- list_for_each_entry_safe(watch, next, &inode->inotify_watches, i_list) {454454- struct inotify_handle *ih = watch->ih;455455- mutex_lock(&ih->mutex);456456- inotify_remove_watch_locked(ih, watch);457457- mutex_unlock(&ih->mutex);458458- }459459- mutex_unlock(&inode->inotify_mutex);460460-}461461-EXPORT_SYMBOL_GPL(inotify_inode_is_dead);462462-463463-/* Kernel Consumer API */464464-465465-/**466466- * inotify_init - allocate and initialize an inotify instance467467- * @ops: caller's inotify operations468468- */469469-struct inotify_handle *inotify_init(const struct inotify_operations *ops)470470-{471471- struct inotify_handle *ih;472472-473473- ih = kmalloc(sizeof(struct inotify_handle), GFP_KERNEL);474474- if (unlikely(!ih))475475- return ERR_PTR(-ENOMEM);476476-477477- idr_init(&ih->idr);478478- INIT_LIST_HEAD(&ih->watches);479479- mutex_init(&ih->mutex);480480- ih->last_wd = 0;481481- ih->in_ops = ops;482482- atomic_set(&ih->count, 0);483483- get_inotify_handle(ih);484484-485485- return ih;486486-}487487-EXPORT_SYMBOL_GPL(inotify_init);488488-489489-/**490490- * inotify_init_watch - initialize an inotify watch491491- * @watch: watch to initialize492492- */493493-void inotify_init_watch(struct inotify_watch *watch)494494-{495495- INIT_LIST_HEAD(&watch->h_list);496496- INIT_LIST_HEAD(&watch->i_list);497497- atomic_set(&watch->count, 0);498498- get_inotify_watch(watch); /* initial get */499499-}500500-EXPORT_SYMBOL_GPL(inotify_init_watch);501501-502502-/*503503- * Watch removals suck violently. To kick the watch out we need (in this504504- * order) inode->inotify_mutex and ih->mutex. That's fine if we have505505- * a hold on inode; however, for all other cases we need to make damn sure506506- * we don't race with umount. We can *NOT* just grab a reference to a507507- * watch - inotify_unmount_inodes() will happily sail past it and we'll end508508- * with reference to inode potentially outliving its superblock. Ideally509509- * we just want to grab an active reference to superblock if we can; that510510- * will make sure we won't go into inotify_umount_inodes() until we are511511- * done. Cleanup is just deactivate_super(). However, that leaves a messy512512- * case - what if we *are* racing with umount() and active references to513513- * superblock can't be acquired anymore? We can bump ->s_count, grab514514- * ->s_umount, which will wait until the superblock is shut down and the515515- * watch in question is pining for fjords.516516- *517517- * And yes, this is far beyond mere "not very pretty"; so's the entire518518- * concept of inotify to start with.519519- */520520-521521-/**522522- * pin_to_kill - pin the watch down for removal523523- * @ih: inotify handle524524- * @watch: watch to kill525525- *526526- * Called with ih->mutex held, drops it. Possible return values:527527- * 0 - nothing to do, it has died528528- * 1 - remove it, drop the reference and deactivate_super()529529- */530530-static int pin_to_kill(struct inotify_handle *ih, struct inotify_watch *watch)531531-{532532- struct super_block *sb = watch->inode->i_sb;533533-534534- if (atomic_inc_not_zero(&sb->s_active)) {535535- get_inotify_watch(watch);536536- mutex_unlock(&ih->mutex);537537- return 1; /* the best outcome */538538- }539539- spin_lock(&sb_lock);540540- sb->s_count++;541541- spin_unlock(&sb_lock);542542- mutex_unlock(&ih->mutex); /* can't grab ->s_umount under it */543543- down_read(&sb->s_umount);544544- /* fs is already shut down; the watch is dead */545545- drop_super(sb);546546- return 0;547547-}548548-549549-static void unpin_and_kill(struct inotify_watch *watch)550550-{551551- struct super_block *sb = watch->inode->i_sb;552552- put_inotify_watch(watch);553553- deactivate_super(sb);554554-}555555-556556-/**557557- * inotify_destroy - clean up and destroy an inotify instance558558- * @ih: inotify handle559559- */560560-void inotify_destroy(struct inotify_handle *ih)561561-{562562- /*563563- * Destroy all of the watches for this handle. Unfortunately, not very564564- * pretty. We cannot do a simple iteration over the list, because we565565- * do not know the inode until we iterate to the watch. But we need to566566- * hold inode->inotify_mutex before ih->mutex. The following works.567567- *568568- * AV: it had to become even uglier to start working ;-/569569- */570570- while (1) {571571- struct inotify_watch *watch;572572- struct list_head *watches;573573- struct super_block *sb;574574- struct inode *inode;575575-576576- mutex_lock(&ih->mutex);577577- watches = &ih->watches;578578- if (list_empty(watches)) {579579- mutex_unlock(&ih->mutex);580580- break;581581- }582582- watch = list_first_entry(watches, struct inotify_watch, h_list);583583- sb = watch->inode->i_sb;584584- if (!pin_to_kill(ih, watch))585585- continue;586586-587587- inode = watch->inode;588588- mutex_lock(&inode->inotify_mutex);589589- mutex_lock(&ih->mutex);590590-591591- /* make sure we didn't race with another list removal */592592- if (likely(idr_find(&ih->idr, watch->wd))) {593593- remove_watch_no_event(watch, ih);594594- put_inotify_watch(watch);595595- }596596-597597- mutex_unlock(&ih->mutex);598598- mutex_unlock(&inode->inotify_mutex);599599- unpin_and_kill(watch);600600- }601601-602602- /* free this handle: the put matching the get in inotify_init() */603603- put_inotify_handle(ih);604604-}605605-EXPORT_SYMBOL_GPL(inotify_destroy);606606-607607-/**608608- * inotify_find_watch - find an existing watch for an (ih,inode) pair609609- * @ih: inotify handle610610- * @inode: inode to watch611611- * @watchp: pointer to existing inotify_watch612612- *613613- * Caller must pin given inode (via nameidata).614614- */615615-s32 inotify_find_watch(struct inotify_handle *ih, struct inode *inode,616616- struct inotify_watch **watchp)617617-{618618- struct inotify_watch *old;619619- int ret = -ENOENT;620620-621621- mutex_lock(&inode->inotify_mutex);622622- mutex_lock(&ih->mutex);623623-624624- old = inode_find_handle(inode, ih);625625- if (unlikely(old)) {626626- get_inotify_watch(old); /* caller must put watch */627627- *watchp = old;628628- ret = old->wd;629629- }630630-631631- mutex_unlock(&ih->mutex);632632- mutex_unlock(&inode->inotify_mutex);633633-634634- return ret;635635-}636636-EXPORT_SYMBOL_GPL(inotify_find_watch);637637-638638-/**639639- * inotify_find_update_watch - find and update the mask of an existing watch640640- * @ih: inotify handle641641- * @inode: inode's watch to update642642- * @mask: mask of events to watch643643- *644644- * Caller must pin given inode (via nameidata).645645- */646646-s32 inotify_find_update_watch(struct inotify_handle *ih, struct inode *inode,647647- u32 mask)648648-{649649- struct inotify_watch *old;650650- int mask_add = 0;651651- int ret;652652-653653- if (mask & IN_MASK_ADD)654654- mask_add = 1;655655-656656- /* don't allow invalid bits: we don't want flags set */657657- mask &= IN_ALL_EVENTS | IN_ONESHOT;658658- if (unlikely(!mask))659659- return -EINVAL;660660-661661- mutex_lock(&inode->inotify_mutex);662662- mutex_lock(&ih->mutex);663663-664664- /*665665- * Handle the case of re-adding a watch on an (inode,ih) pair that we666666- * are already watching. We just update the mask and return its wd.667667- */668668- old = inode_find_handle(inode, ih);669669- if (unlikely(!old)) {670670- ret = -ENOENT;671671- goto out;672672- }673673-674674- if (mask_add)675675- old->mask |= mask;676676- else677677- old->mask = mask;678678- ret = old->wd;679679-out:680680- mutex_unlock(&ih->mutex);681681- mutex_unlock(&inode->inotify_mutex);682682- return ret;683683-}684684-EXPORT_SYMBOL_GPL(inotify_find_update_watch);685685-686686-/**687687- * inotify_add_watch - add a watch to an inotify instance688688- * @ih: inotify handle689689- * @watch: caller allocated watch structure690690- * @inode: inode to watch691691- * @mask: mask of events to watch692692- *693693- * Caller must pin given inode (via nameidata).694694- * Caller must ensure it only calls inotify_add_watch() once per watch.695695- * Calls inotify_handle_get_wd() so may sleep.696696- */697697-s32 inotify_add_watch(struct inotify_handle *ih, struct inotify_watch *watch,698698- struct inode *inode, u32 mask)699699-{700700- int ret = 0;701701- int newly_watched;702702-703703- /* don't allow invalid bits: we don't want flags set */704704- mask &= IN_ALL_EVENTS | IN_ONESHOT;705705- if (unlikely(!mask))706706- return -EINVAL;707707- watch->mask = mask;708708-709709- mutex_lock(&inode->inotify_mutex);710710- mutex_lock(&ih->mutex);711711-712712- /* Initialize a new watch */713713- ret = inotify_handle_get_wd(ih, watch);714714- if (unlikely(ret))715715- goto out;716716- ret = watch->wd;717717-718718- /* save a reference to handle and bump the count to make it official */719719- get_inotify_handle(ih);720720- watch->ih = ih;721721-722722- /*723723- * Save a reference to the inode and bump the ref count to make it724724- * official. We hold a reference to nameidata, which makes this safe.725725- */726726- watch->inode = igrab(inode);727727-728728- /* Add the watch to the handle's and the inode's list */729729- newly_watched = !inotify_inode_watched(inode);730730- list_add(&watch->h_list, &ih->watches);731731- list_add(&watch->i_list, &inode->inotify_watches);732732- /*733733- * Set child flags _after_ adding the watch, so there is no race734734- * windows where newly instantiated children could miss their parent's735735- * watched flag.736736- */737737- if (newly_watched)738738- set_dentry_child_flags(inode, 1);739739-740740-out:741741- mutex_unlock(&ih->mutex);742742- mutex_unlock(&inode->inotify_mutex);743743- return ret;744744-}745745-EXPORT_SYMBOL_GPL(inotify_add_watch);746746-747747-/**748748- * inotify_clone_watch - put the watch next to existing one749749- * @old: already installed watch750750- * @new: new watch751751- *752752- * Caller must hold the inotify_mutex of inode we are dealing with;753753- * it is expected to remove the old watch before unlocking the inode.754754- */755755-s32 inotify_clone_watch(struct inotify_watch *old, struct inotify_watch *new)756756-{757757- struct inotify_handle *ih = old->ih;758758- int ret = 0;759759-760760- new->mask = old->mask;761761- new->ih = ih;762762-763763- mutex_lock(&ih->mutex);764764-765765- /* Initialize a new watch */766766- ret = inotify_handle_get_wd(ih, new);767767- if (unlikely(ret))768768- goto out;769769- ret = new->wd;770770-771771- get_inotify_handle(ih);772772-773773- new->inode = igrab(old->inode);774774-775775- list_add(&new->h_list, &ih->watches);776776- list_add(&new->i_list, &old->inode->inotify_watches);777777-out:778778- mutex_unlock(&ih->mutex);779779- return ret;780780-}781781-782782-void inotify_evict_watch(struct inotify_watch *watch)783783-{784784- get_inotify_watch(watch);785785- mutex_lock(&watch->ih->mutex);786786- inotify_remove_watch_locked(watch->ih, watch);787787- mutex_unlock(&watch->ih->mutex);788788-}789789-790790-/**791791- * inotify_rm_wd - remove a watch from an inotify instance792792- * @ih: inotify handle793793- * @wd: watch descriptor to remove794794- *795795- * Can sleep.796796- */797797-int inotify_rm_wd(struct inotify_handle *ih, u32 wd)798798-{799799- struct inotify_watch *watch;800800- struct super_block *sb;801801- struct inode *inode;802802-803803- mutex_lock(&ih->mutex);804804- watch = idr_find(&ih->idr, wd);805805- if (unlikely(!watch)) {806806- mutex_unlock(&ih->mutex);807807- return -EINVAL;808808- }809809- sb = watch->inode->i_sb;810810- if (!pin_to_kill(ih, watch))811811- return 0;812812-813813- inode = watch->inode;814814-815815- mutex_lock(&inode->inotify_mutex);816816- mutex_lock(&ih->mutex);817817-818818- /* make sure that we did not race */819819- if (likely(idr_find(&ih->idr, wd) == watch))820820- inotify_remove_watch_locked(ih, watch);821821-822822- mutex_unlock(&ih->mutex);823823- mutex_unlock(&inode->inotify_mutex);824824- unpin_and_kill(watch);825825-826826- return 0;827827-}828828-EXPORT_SYMBOL_GPL(inotify_rm_wd);829829-830830-/**831831- * inotify_rm_watch - remove a watch from an inotify instance832832- * @ih: inotify handle833833- * @watch: watch to remove834834- *835835- * Can sleep.836836- */837837-int inotify_rm_watch(struct inotify_handle *ih,838838- struct inotify_watch *watch)839839-{840840- return inotify_rm_wd(ih, watch->wd);841841-}842842-EXPORT_SYMBOL_GPL(inotify_rm_watch);843843-844844-/*845845- * inotify_setup - core initialization function846846- */847847-static int __init inotify_setup(void)848848-{849849- BUILD_BUG_ON(IN_ACCESS != FS_ACCESS);850850- BUILD_BUG_ON(IN_MODIFY != FS_MODIFY);851851- BUILD_BUG_ON(IN_ATTRIB != FS_ATTRIB);852852- BUILD_BUG_ON(IN_CLOSE_WRITE != FS_CLOSE_WRITE);853853- BUILD_BUG_ON(IN_CLOSE_NOWRITE != FS_CLOSE_NOWRITE);854854- BUILD_BUG_ON(IN_OPEN != FS_OPEN);855855- BUILD_BUG_ON(IN_MOVED_FROM != FS_MOVED_FROM);856856- BUILD_BUG_ON(IN_MOVED_TO != FS_MOVED_TO);857857- BUILD_BUG_ON(IN_CREATE != FS_CREATE);858858- BUILD_BUG_ON(IN_DELETE != FS_DELETE);859859- BUILD_BUG_ON(IN_DELETE_SELF != FS_DELETE_SELF);860860- BUILD_BUG_ON(IN_MOVE_SELF != FS_MOVE_SELF);861861- BUILD_BUG_ON(IN_Q_OVERFLOW != FS_Q_OVERFLOW);862862-863863- BUILD_BUG_ON(IN_UNMOUNT != FS_UNMOUNT);864864- BUILD_BUG_ON(IN_ISDIR != FS_IN_ISDIR);865865- BUILD_BUG_ON(IN_IGNORED != FS_IN_IGNORED);866866- BUILD_BUG_ON(IN_ONESHOT != FS_IN_ONESHOT);867867-868868- atomic_set(&inotify_cookie, 0);869869-870870- return 0;871871-}872872-873873-module_init(inotify_setup);
···771771 struct hlist_head i_fsnotify_mark_entries; /* fsnotify mark entries */772772#endif773773774774-#ifdef CONFIG_INOTIFY775775- struct list_head inotify_watches; /* watches on this inode */776776- struct mutex inotify_mutex; /* protects the watches list */777777-#endif778778-779774 unsigned long i_state;780775 unsigned long dirtied_when; /* jiffies of first dirtying */781776