inode.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  1. /*
  2. * linux/fs/inode.c
  3. *
  4. * (C) 1997 Linus Torvalds
  5. */
  6. #include <linux/fs.h>
  7. #include <linux/mm.h>
  8. #include <linux/dcache.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/writeback.h>
  12. #include <linux/module.h>
  13. #include <linux/backing-dev.h>
  14. #include <linux/wait.h>
  15. #include <linux/rwsem.h>
  16. #include <linux/hash.h>
  17. #include <linux/swap.h>
  18. #include <linux/security.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/cdev.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/fsnotify.h>
  23. #include <linux/mount.h>
  24. #include <linux/async.h>
  25. #include <linux/posix_acl.h>
  26. #include <linux/ima.h>
  27. /*
  28. * This is needed for the following functions:
  29. * - inode_has_buffers
  30. * - invalidate_bdev
  31. *
  32. * FIXME: remove all knowledge of the buffer layer from this file
  33. */
  34. #include <linux/buffer_head.h>
  35. /*
  36. * New inode.c implementation.
  37. *
  38. * This implementation has the basic premise of trying
  39. * to be extremely low-overhead and SMP-safe, yet be
  40. * simple enough to be "obviously correct".
  41. *
  42. * Famous last words.
  43. */
  44. /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
  45. /* #define INODE_PARANOIA 1 */
  46. /* #define INODE_DEBUG 1 */
  47. /*
  48. * Inode lookup is no longer as critical as it used to be:
  49. * most of the lookups are going to be through the dcache.
  50. */
  51. #define I_HASHBITS i_hash_shift
  52. #define I_HASHMASK i_hash_mask
  53. static unsigned int i_hash_mask __read_mostly;
  54. static unsigned int i_hash_shift __read_mostly;
  55. /*
  56. * Each inode can be on two separate lists. One is
  57. * the hash list of the inode, used for lookups. The
  58. * other linked list is the "type" list:
  59. * "in_use" - valid inode, i_count > 0, i_nlink > 0
  60. * "dirty" - as "in_use" but also dirty
  61. * "unused" - valid inode, i_count = 0
  62. *
  63. * A "dirty" list is maintained for each super block,
  64. * allowing for low-overhead inode sync() operations.
  65. */
  66. static LIST_HEAD(inode_lru);
  67. static struct hlist_head *inode_hashtable __read_mostly;
  68. /*
  69. * A simple spinlock to protect the list manipulations.
  70. *
  71. * NOTE! You also have to own the lock if you change
  72. * the i_state of an inode while it is in use..
  73. */
  74. DEFINE_SPINLOCK(inode_lock);
  75. /*
  76. * iprune_sem provides exclusion between the kswapd or try_to_free_pages
  77. * icache shrinking path, and the umount path. Without this exclusion,
  78. * by the time prune_icache calls iput for the inode whose pages it has
  79. * been invalidating, or by the time it calls clear_inode & destroy_inode
  80. * from its final dispose_list, the struct super_block they refer to
  81. * (for inode->i_sb->s_op) may already have been freed and reused.
  82. *
  83. * We make this an rwsem because the fastpath is icache shrinking. In
  84. * some cases a filesystem may be doing a significant amount of work in
  85. * its inode reclaim code, so this should improve parallelism.
  86. */
  87. static DECLARE_RWSEM(iprune_sem);
  88. /*
  89. * Statistics gathering..
  90. */
  91. struct inodes_stat_t inodes_stat;
  92. static DEFINE_PER_CPU(unsigned int, nr_inodes);
  93. static struct kmem_cache *inode_cachep __read_mostly;
  94. static int get_nr_inodes(void)
  95. {
  96. int i;
  97. int sum = 0;
  98. for_each_possible_cpu(i)
  99. sum += per_cpu(nr_inodes, i);
  100. return sum < 0 ? 0 : sum;
  101. }
  102. static inline int get_nr_inodes_unused(void)
  103. {
  104. return inodes_stat.nr_unused;
  105. }
  106. int get_nr_dirty_inodes(void)
  107. {
  108. /* not actually dirty inodes, but a wild approximation */
  109. int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
  110. return nr_dirty > 0 ? nr_dirty : 0;
  111. }
  112. /*
  113. * Handle nr_inode sysctl
  114. */
  115. #ifdef CONFIG_SYSCTL
  116. int proc_nr_inodes(ctl_table *table, int write,
  117. void __user *buffer, size_t *lenp, loff_t *ppos)
  118. {
  119. inodes_stat.nr_inodes = get_nr_inodes();
  120. return proc_dointvec(table, write, buffer, lenp, ppos);
  121. }
  122. #endif
  123. static void wake_up_inode(struct inode *inode)
  124. {
  125. /*
  126. * Prevent speculative execution through spin_unlock(&inode_lock);
  127. */
  128. smp_mb();
  129. wake_up_bit(&inode->i_state, __I_NEW);
  130. }
  131. /**
  132. * inode_init_always - perform inode structure intialisation
  133. * @sb: superblock inode belongs to
  134. * @inode: inode to initialise
  135. *
  136. * These are initializations that need to be done on every inode
  137. * allocation as the fields are not initialised by slab allocation.
  138. */
  139. int inode_init_always(struct super_block *sb, struct inode *inode)
  140. {
  141. static const struct address_space_operations empty_aops;
  142. static const struct inode_operations empty_iops;
  143. static const struct file_operations empty_fops;
  144. struct address_space *const mapping = &inode->i_data;
  145. inode->i_sb = sb;
  146. inode->i_blkbits = sb->s_blocksize_bits;
  147. inode->i_flags = 0;
  148. atomic_set(&inode->i_count, 1);
  149. inode->i_op = &empty_iops;
  150. inode->i_fop = &empty_fops;
  151. inode->i_nlink = 1;
  152. inode->i_uid = 0;
  153. inode->i_gid = 0;
  154. atomic_set(&inode->i_writecount, 0);
  155. inode->i_size = 0;
  156. inode->i_blocks = 0;
  157. inode->i_bytes = 0;
  158. inode->i_generation = 0;
  159. #ifdef CONFIG_QUOTA
  160. memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
  161. #endif
  162. inode->i_pipe = NULL;
  163. inode->i_bdev = NULL;
  164. inode->i_cdev = NULL;
  165. inode->i_rdev = 0;
  166. inode->dirtied_when = 0;
  167. if (security_inode_alloc(inode))
  168. goto out;
  169. spin_lock_init(&inode->i_lock);
  170. lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
  171. mutex_init(&inode->i_mutex);
  172. lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
  173. init_rwsem(&inode->i_alloc_sem);
  174. lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
  175. mapping->a_ops = &empty_aops;
  176. mapping->host = inode;
  177. mapping->flags = 0;
  178. mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
  179. mapping->assoc_mapping = NULL;
  180. mapping->backing_dev_info = &default_backing_dev_info;
  181. mapping->writeback_index = 0;
  182. /*
  183. * If the block_device provides a backing_dev_info for client
  184. * inodes then use that. Otherwise the inode share the bdev's
  185. * backing_dev_info.
  186. */
  187. if (sb->s_bdev) {
  188. struct backing_dev_info *bdi;
  189. bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  190. mapping->backing_dev_info = bdi;
  191. }
  192. inode->i_private = NULL;
  193. inode->i_mapping = mapping;
  194. #ifdef CONFIG_FS_POSIX_ACL
  195. inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
  196. #endif
  197. #ifdef CONFIG_FSNOTIFY
  198. inode->i_fsnotify_mask = 0;
  199. #endif
  200. this_cpu_inc(nr_inodes);
  201. return 0;
  202. out:
  203. return -ENOMEM;
  204. }
  205. EXPORT_SYMBOL(inode_init_always);
  206. static struct inode *alloc_inode(struct super_block *sb)
  207. {
  208. struct inode *inode;
  209. if (sb->s_op->alloc_inode)
  210. inode = sb->s_op->alloc_inode(sb);
  211. else
  212. inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
  213. if (!inode)
  214. return NULL;
  215. if (unlikely(inode_init_always(sb, inode))) {
  216. if (inode->i_sb->s_op->destroy_inode)
  217. inode->i_sb->s_op->destroy_inode(inode);
  218. else
  219. kmem_cache_free(inode_cachep, inode);
  220. return NULL;
  221. }
  222. return inode;
  223. }
  224. void free_inode_nonrcu(struct inode *inode)
  225. {
  226. kmem_cache_free(inode_cachep, inode);
  227. }
  228. EXPORT_SYMBOL(free_inode_nonrcu);
  229. void __destroy_inode(struct inode *inode)
  230. {
  231. BUG_ON(inode_has_buffers(inode));
  232. security_inode_free(inode);
  233. fsnotify_inode_delete(inode);
  234. #ifdef CONFIG_FS_POSIX_ACL
  235. if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
  236. posix_acl_release(inode->i_acl);
  237. if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
  238. posix_acl_release(inode->i_default_acl);
  239. #endif
  240. this_cpu_dec(nr_inodes);
  241. }
  242. EXPORT_SYMBOL(__destroy_inode);
  243. static void i_callback(struct rcu_head *head)
  244. {
  245. struct inode *inode = container_of(head, struct inode, i_rcu);
  246. INIT_LIST_HEAD(&inode->i_dentry);
  247. kmem_cache_free(inode_cachep, inode);
  248. }
  249. static void destroy_inode(struct inode *inode)
  250. {
  251. BUG_ON(!list_empty(&inode->i_lru));
  252. __destroy_inode(inode);
  253. if (inode->i_sb->s_op->destroy_inode)
  254. inode->i_sb->s_op->destroy_inode(inode);
  255. else
  256. call_rcu(&inode->i_rcu, i_callback);
  257. }
  258. void address_space_init_once(struct address_space *mapping)
  259. {
  260. memset(mapping, 0, sizeof(*mapping));
  261. INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
  262. spin_lock_init(&mapping->tree_lock);
  263. spin_lock_init(&mapping->i_mmap_lock);
  264. INIT_LIST_HEAD(&mapping->private_list);
  265. spin_lock_init(&mapping->private_lock);
  266. INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
  267. INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
  268. mutex_init(&mapping->unmap_mutex);
  269. }
  270. EXPORT_SYMBOL(address_space_init_once);
  271. /*
  272. * These are initializations that only need to be done
  273. * once, because the fields are idempotent across use
  274. * of the inode, so let the slab aware of that.
  275. */
  276. void inode_init_once(struct inode *inode)
  277. {
  278. memset(inode, 0, sizeof(*inode));
  279. INIT_HLIST_NODE(&inode->i_hash);
  280. INIT_LIST_HEAD(&inode->i_dentry);
  281. INIT_LIST_HEAD(&inode->i_devices);
  282. INIT_LIST_HEAD(&inode->i_wb_list);
  283. INIT_LIST_HEAD(&inode->i_lru);
  284. address_space_init_once(&inode->i_data);
  285. i_size_ordered_init(inode);
  286. #ifdef CONFIG_FSNOTIFY
  287. INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
  288. #endif
  289. }
  290. EXPORT_SYMBOL(inode_init_once);
  291. static void init_once(void *foo)
  292. {
  293. struct inode *inode = (struct inode *) foo;
  294. inode_init_once(inode);
  295. }
  296. /*
  297. * inode_lock must be held
  298. */
  299. void __iget(struct inode *inode)
  300. {
  301. atomic_inc(&inode->i_count);
  302. }
  303. /*
  304. * get additional reference to inode; caller must already hold one.
  305. */
  306. void ihold(struct inode *inode)
  307. {
  308. WARN_ON(atomic_inc_return(&inode->i_count) < 2);
  309. }
  310. EXPORT_SYMBOL(ihold);
  311. static void inode_lru_list_add(struct inode *inode)
  312. {
  313. if (list_empty(&inode->i_lru)) {
  314. list_add(&inode->i_lru, &inode_lru);
  315. inodes_stat.nr_unused++;
  316. }
  317. }
  318. static void inode_lru_list_del(struct inode *inode)
  319. {
  320. if (!list_empty(&inode->i_lru)) {
  321. list_del_init(&inode->i_lru);
  322. inodes_stat.nr_unused--;
  323. }
  324. }
  325. static inline void __inode_sb_list_add(struct inode *inode)
  326. {
  327. list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
  328. }
  329. /**
  330. * inode_sb_list_add - add inode to the superblock list of inodes
  331. * @inode: inode to add
  332. */
  333. void inode_sb_list_add(struct inode *inode)
  334. {
  335. spin_lock(&inode_lock);
  336. __inode_sb_list_add(inode);
  337. spin_unlock(&inode_lock);
  338. }
  339. EXPORT_SYMBOL_GPL(inode_sb_list_add);
  340. static inline void __inode_sb_list_del(struct inode *inode)
  341. {
  342. list_del_init(&inode->i_sb_list);
  343. }
  344. static unsigned long hash(struct super_block *sb, unsigned long hashval)
  345. {
  346. unsigned long tmp;
  347. tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
  348. L1_CACHE_BYTES;
  349. tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
  350. return tmp & I_HASHMASK;
  351. }
  352. /**
  353. * __insert_inode_hash - hash an inode
  354. * @inode: unhashed inode
  355. * @hashval: unsigned long value used to locate this object in the
  356. * inode_hashtable.
  357. *
  358. * Add an inode to the inode hash for this superblock.
  359. */
  360. void __insert_inode_hash(struct inode *inode, unsigned long hashval)
  361. {
  362. struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
  363. spin_lock(&inode_lock);
  364. hlist_add_head(&inode->i_hash, b);
  365. spin_unlock(&inode_lock);
  366. }
  367. EXPORT_SYMBOL(__insert_inode_hash);
  368. /**
  369. * __remove_inode_hash - remove an inode from the hash
  370. * @inode: inode to unhash
  371. *
  372. * Remove an inode from the superblock.
  373. */
  374. static void __remove_inode_hash(struct inode *inode)
  375. {
  376. hlist_del_init(&inode->i_hash);
  377. }
  378. /**
  379. * remove_inode_hash - remove an inode from the hash
  380. * @inode: inode to unhash
  381. *
  382. * Remove an inode from the superblock.
  383. */
  384. void remove_inode_hash(struct inode *inode)
  385. {
  386. spin_lock(&inode_lock);
  387. hlist_del_init(&inode->i_hash);
  388. spin_unlock(&inode_lock);
  389. }
  390. EXPORT_SYMBOL(remove_inode_hash);
  391. void end_writeback(struct inode *inode)
  392. {
  393. might_sleep();
  394. BUG_ON(inode->i_data.nrpages);
  395. BUG_ON(!list_empty(&inode->i_data.private_list));
  396. BUG_ON(!(inode->i_state & I_FREEING));
  397. BUG_ON(inode->i_state & I_CLEAR);
  398. inode_sync_wait(inode);
  399. /* don't need i_lock here, no concurrent mods to i_state */
  400. inode->i_state = I_FREEING | I_CLEAR;
  401. }
  402. EXPORT_SYMBOL(end_writeback);
  403. static void evict(struct inode *inode)
  404. {
  405. const struct super_operations *op = inode->i_sb->s_op;
  406. if (op->evict_inode) {
  407. op->evict_inode(inode);
  408. } else {
  409. if (inode->i_data.nrpages)
  410. truncate_inode_pages(&inode->i_data, 0);
  411. end_writeback(inode);
  412. }
  413. if (S_ISBLK(inode->i_mode) && inode->i_bdev)
  414. bd_forget(inode);
  415. if (S_ISCHR(inode->i_mode) && inode->i_cdev)
  416. cd_forget(inode);
  417. }
  418. /*
  419. * dispose_list - dispose of the contents of a local list
  420. * @head: the head of the list to free
  421. *
  422. * Dispose-list gets a local list with local inodes in it, so it doesn't
  423. * need to worry about list corruption and SMP locks.
  424. */
  425. static void dispose_list(struct list_head *head)
  426. {
  427. while (!list_empty(head)) {
  428. struct inode *inode;
  429. inode = list_first_entry(head, struct inode, i_lru);
  430. list_del_init(&inode->i_lru);
  431. evict(inode);
  432. spin_lock(&inode_lock);
  433. __remove_inode_hash(inode);
  434. __inode_sb_list_del(inode);
  435. spin_unlock(&inode_lock);
  436. wake_up_inode(inode);
  437. destroy_inode(inode);
  438. }
  439. }
  440. /**
  441. * evict_inodes - evict all evictable inodes for a superblock
  442. * @sb: superblock to operate on
  443. *
  444. * Make sure that no inodes with zero refcount are retained. This is
  445. * called by superblock shutdown after having MS_ACTIVE flag removed,
  446. * so any inode reaching zero refcount during or after that call will
  447. * be immediately evicted.
  448. */
  449. void evict_inodes(struct super_block *sb)
  450. {
  451. struct inode *inode, *next;
  452. LIST_HEAD(dispose);
  453. down_write(&iprune_sem);
  454. spin_lock(&inode_lock);
  455. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  456. if (atomic_read(&inode->i_count))
  457. continue;
  458. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
  459. WARN_ON(1);
  460. continue;
  461. }
  462. inode->i_state |= I_FREEING;
  463. /*
  464. * Move the inode off the IO lists and LRU once I_FREEING is
  465. * set so that it won't get moved back on there if it is dirty.
  466. */
  467. list_move(&inode->i_lru, &dispose);
  468. list_del_init(&inode->i_wb_list);
  469. if (!(inode->i_state & (I_DIRTY | I_SYNC)))
  470. inodes_stat.nr_unused--;
  471. }
  472. spin_unlock(&inode_lock);
  473. dispose_list(&dispose);
  474. up_write(&iprune_sem);
  475. }
  476. /**
  477. * invalidate_inodes - attempt to free all inodes on a superblock
  478. * @sb: superblock to operate on
  479. *
  480. * Attempts to free all inodes for a given superblock. If there were any
  481. * busy inodes return a non-zero value, else zero.
  482. */
  483. int invalidate_inodes(struct super_block *sb)
  484. {
  485. int busy = 0;
  486. struct inode *inode, *next;
  487. LIST_HEAD(dispose);
  488. down_write(&iprune_sem);
  489. spin_lock(&inode_lock);
  490. list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
  491. if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
  492. continue;
  493. if (atomic_read(&inode->i_count)) {
  494. busy = 1;
  495. continue;
  496. }
  497. inode->i_state |= I_FREEING;
  498. /*
  499. * Move the inode off the IO lists and LRU once I_FREEING is
  500. * set so that it won't get moved back on there if it is dirty.
  501. */
  502. list_move(&inode->i_lru, &dispose);
  503. list_del_init(&inode->i_wb_list);
  504. if (!(inode->i_state & (I_DIRTY | I_SYNC)))
  505. inodes_stat.nr_unused--;
  506. }
  507. spin_unlock(&inode_lock);
  508. dispose_list(&dispose);
  509. up_write(&iprune_sem);
  510. return busy;
  511. }
  512. static int can_unuse(struct inode *inode)
  513. {
  514. if (inode->i_state & ~I_REFERENCED)
  515. return 0;
  516. if (inode_has_buffers(inode))
  517. return 0;
  518. if (atomic_read(&inode->i_count))
  519. return 0;
  520. if (inode->i_data.nrpages)
  521. return 0;
  522. return 1;
  523. }
  524. /*
  525. * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
  526. * temporary list and then are freed outside inode_lock by dispose_list().
  527. *
  528. * Any inodes which are pinned purely because of attached pagecache have their
  529. * pagecache removed. If the inode has metadata buffers attached to
  530. * mapping->private_list then try to remove them.
  531. *
  532. * If the inode has the I_REFERENCED flag set, then it means that it has been
  533. * used recently - the flag is set in iput_final(). When we encounter such an
  534. * inode, clear the flag and move it to the back of the LRU so it gets another
  535. * pass through the LRU before it gets reclaimed. This is necessary because of
  536. * the fact we are doing lazy LRU updates to minimise lock contention so the
  537. * LRU does not have strict ordering. Hence we don't want to reclaim inodes
  538. * with this flag set because they are the inodes that are out of order.
  539. */
  540. static void prune_icache(int nr_to_scan)
  541. {
  542. LIST_HEAD(freeable);
  543. int nr_scanned;
  544. unsigned long reap = 0;
  545. down_read(&iprune_sem);
  546. spin_lock(&inode_lock);
  547. for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
  548. struct inode *inode;
  549. if (list_empty(&inode_lru))
  550. break;
  551. inode = list_entry(inode_lru.prev, struct inode, i_lru);
  552. /*
  553. * Referenced or dirty inodes are still in use. Give them
  554. * another pass through the LRU as we canot reclaim them now.
  555. */
  556. if (atomic_read(&inode->i_count) ||
  557. (inode->i_state & ~I_REFERENCED)) {
  558. list_del_init(&inode->i_lru);
  559. inodes_stat.nr_unused--;
  560. continue;
  561. }
  562. /* recently referenced inodes get one more pass */
  563. if (inode->i_state & I_REFERENCED) {
  564. list_move(&inode->i_lru, &inode_lru);
  565. inode->i_state &= ~I_REFERENCED;
  566. continue;
  567. }
  568. if (inode_has_buffers(inode) || inode->i_data.nrpages) {
  569. __iget(inode);
  570. spin_unlock(&inode_lock);
  571. if (remove_inode_buffers(inode))
  572. reap += invalidate_mapping_pages(&inode->i_data,
  573. 0, -1);
  574. iput(inode);
  575. spin_lock(&inode_lock);
  576. if (inode != list_entry(inode_lru.next,
  577. struct inode, i_lru))
  578. continue; /* wrong inode or list_empty */
  579. if (!can_unuse(inode))
  580. continue;
  581. }
  582. WARN_ON(inode->i_state & I_NEW);
  583. inode->i_state |= I_FREEING;
  584. /*
  585. * Move the inode off the IO lists and LRU once I_FREEING is
  586. * set so that it won't get moved back on there if it is dirty.
  587. */
  588. list_move(&inode->i_lru, &freeable);
  589. list_del_init(&inode->i_wb_list);
  590. inodes_stat.nr_unused--;
  591. }
  592. if (current_is_kswapd())
  593. __count_vm_events(KSWAPD_INODESTEAL, reap);
  594. else
  595. __count_vm_events(PGINODESTEAL, reap);
  596. spin_unlock(&inode_lock);
  597. dispose_list(&freeable);
  598. up_read(&iprune_sem);
  599. }
  600. /*
  601. * shrink_icache_memory() will attempt to reclaim some unused inodes. Here,
  602. * "unused" means that no dentries are referring to the inodes: the files are
  603. * not open and the dcache references to those inodes have already been
  604. * reclaimed.
  605. *
  606. * This function is passed the number of inodes to scan, and it returns the
  607. * total number of remaining possibly-reclaimable inodes.
  608. */
  609. static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
  610. {
  611. if (nr) {
  612. /*
  613. * Nasty deadlock avoidance. We may hold various FS locks,
  614. * and we don't want to recurse into the FS that called us
  615. * in clear_inode() and friends..
  616. */
  617. if (!(gfp_mask & __GFP_FS))
  618. return -1;
  619. prune_icache(nr);
  620. }
  621. return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
  622. }
  623. static struct shrinker icache_shrinker = {
  624. .shrink = shrink_icache_memory,
  625. .seeks = DEFAULT_SEEKS,
  626. };
  627. static void __wait_on_freeing_inode(struct inode *inode);
  628. /*
  629. * Called with the inode lock held.
  630. */
  631. static struct inode *find_inode(struct super_block *sb,
  632. struct hlist_head *head,
  633. int (*test)(struct inode *, void *),
  634. void *data)
  635. {
  636. struct hlist_node *node;
  637. struct inode *inode = NULL;
  638. repeat:
  639. hlist_for_each_entry(inode, node, head, i_hash) {
  640. if (inode->i_sb != sb)
  641. continue;
  642. if (!test(inode, data))
  643. continue;
  644. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  645. __wait_on_freeing_inode(inode);
  646. goto repeat;
  647. }
  648. __iget(inode);
  649. return inode;
  650. }
  651. return NULL;
  652. }
  653. /*
  654. * find_inode_fast is the fast path version of find_inode, see the comment at
  655. * iget_locked for details.
  656. */
  657. static struct inode *find_inode_fast(struct super_block *sb,
  658. struct hlist_head *head, unsigned long ino)
  659. {
  660. struct hlist_node *node;
  661. struct inode *inode = NULL;
  662. repeat:
  663. hlist_for_each_entry(inode, node, head, i_hash) {
  664. if (inode->i_ino != ino)
  665. continue;
  666. if (inode->i_sb != sb)
  667. continue;
  668. if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
  669. __wait_on_freeing_inode(inode);
  670. goto repeat;
  671. }
  672. __iget(inode);
  673. return inode;
  674. }
  675. return NULL;
  676. }
  677. /*
  678. * Each cpu owns a range of LAST_INO_BATCH numbers.
  679. * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
  680. * to renew the exhausted range.
  681. *
  682. * This does not significantly increase overflow rate because every CPU can
  683. * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
  684. * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
  685. * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
  686. * overflow rate by 2x, which does not seem too significant.
  687. *
  688. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  689. * error if st_ino won't fit in target struct field. Use 32bit counter
  690. * here to attempt to avoid that.
  691. */
  692. #define LAST_INO_BATCH 1024
  693. static DEFINE_PER_CPU(unsigned int, last_ino);
  694. unsigned int get_next_ino(void)
  695. {
  696. unsigned int *p = &get_cpu_var(last_ino);
  697. unsigned int res = *p;
  698. #ifdef CONFIG_SMP
  699. if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
  700. static atomic_t shared_last_ino;
  701. int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
  702. res = next - LAST_INO_BATCH;
  703. }
  704. #endif
  705. *p = ++res;
  706. put_cpu_var(last_ino);
  707. return res;
  708. }
  709. EXPORT_SYMBOL(get_next_ino);
  710. /**
  711. * new_inode - obtain an inode
  712. * @sb: superblock
  713. *
  714. * Allocates a new inode for given superblock. The default gfp_mask
  715. * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
  716. * If HIGHMEM pages are unsuitable or it is known that pages allocated
  717. * for the page cache are not reclaimable or migratable,
  718. * mapping_set_gfp_mask() must be called with suitable flags on the
  719. * newly created inode's mapping
  720. *
  721. */
  722. struct inode *new_inode(struct super_block *sb)
  723. {
  724. struct inode *inode;
  725. spin_lock_prefetch(&inode_lock);
  726. inode = alloc_inode(sb);
  727. if (inode) {
  728. spin_lock(&inode_lock);
  729. __inode_sb_list_add(inode);
  730. inode->i_state = 0;
  731. spin_unlock(&inode_lock);
  732. }
  733. return inode;
  734. }
  735. EXPORT_SYMBOL(new_inode);
  736. void unlock_new_inode(struct inode *inode)
  737. {
  738. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  739. if (S_ISDIR(inode->i_mode)) {
  740. struct file_system_type *type = inode->i_sb->s_type;
  741. /* Set new key only if filesystem hasn't already changed it */
  742. if (!lockdep_match_class(&inode->i_mutex,
  743. &type->i_mutex_key)) {
  744. /*
  745. * ensure nobody is actually holding i_mutex
  746. */
  747. mutex_destroy(&inode->i_mutex);
  748. mutex_init(&inode->i_mutex);
  749. lockdep_set_class(&inode->i_mutex,
  750. &type->i_mutex_dir_key);
  751. }
  752. }
  753. #endif
  754. /*
  755. * This is special! We do not need the spinlock when clearing I_NEW,
  756. * because we're guaranteed that nobody else tries to do anything about
  757. * the state of the inode when it is locked, as we just created it (so
  758. * there can be no old holders that haven't tested I_NEW).
  759. * However we must emit the memory barrier so that other CPUs reliably
  760. * see the clearing of I_NEW after the other inode initialisation has
  761. * completed.
  762. */
  763. smp_mb();
  764. WARN_ON(!(inode->i_state & I_NEW));
  765. inode->i_state &= ~I_NEW;
  766. wake_up_inode(inode);
  767. }
  768. EXPORT_SYMBOL(unlock_new_inode);
  769. /*
  770. * This is called without the inode lock held.. Be careful.
  771. *
  772. * We no longer cache the sb_flags in i_flags - see fs.h
  773. * -- rmk@arm.uk.linux.org
  774. */
  775. static struct inode *get_new_inode(struct super_block *sb,
  776. struct hlist_head *head,
  777. int (*test)(struct inode *, void *),
  778. int (*set)(struct inode *, void *),
  779. void *data)
  780. {
  781. struct inode *inode;
  782. inode = alloc_inode(sb);
  783. if (inode) {
  784. struct inode *old;
  785. spin_lock(&inode_lock);
  786. /* We released the lock, so.. */
  787. old = find_inode(sb, head, test, data);
  788. if (!old) {
  789. if (set(inode, data))
  790. goto set_failed;
  791. hlist_add_head(&inode->i_hash, head);
  792. __inode_sb_list_add(inode);
  793. inode->i_state = I_NEW;
  794. spin_unlock(&inode_lock);
  795. /* Return the locked inode with I_NEW set, the
  796. * caller is responsible for filling in the contents
  797. */
  798. return inode;
  799. }
  800. /*
  801. * Uhhuh, somebody else created the same inode under
  802. * us. Use the old inode instead of the one we just
  803. * allocated.
  804. */
  805. spin_unlock(&inode_lock);
  806. destroy_inode(inode);
  807. inode = old;
  808. wait_on_inode(inode);
  809. }
  810. return inode;
  811. set_failed:
  812. spin_unlock(&inode_lock);
  813. destroy_inode(inode);
  814. return NULL;
  815. }
  816. /*
  817. * get_new_inode_fast is the fast path version of get_new_inode, see the
  818. * comment at iget_locked for details.
  819. */
  820. static struct inode *get_new_inode_fast(struct super_block *sb,
  821. struct hlist_head *head, unsigned long ino)
  822. {
  823. struct inode *inode;
  824. inode = alloc_inode(sb);
  825. if (inode) {
  826. struct inode *old;
  827. spin_lock(&inode_lock);
  828. /* We released the lock, so.. */
  829. old = find_inode_fast(sb, head, ino);
  830. if (!old) {
  831. inode->i_ino = ino;
  832. hlist_add_head(&inode->i_hash, head);
  833. __inode_sb_list_add(inode);
  834. inode->i_state = I_NEW;
  835. spin_unlock(&inode_lock);
  836. /* Return the locked inode with I_NEW set, the
  837. * caller is responsible for filling in the contents
  838. */
  839. return inode;
  840. }
  841. /*
  842. * Uhhuh, somebody else created the same inode under
  843. * us. Use the old inode instead of the one we just
  844. * allocated.
  845. */
  846. spin_unlock(&inode_lock);
  847. destroy_inode(inode);
  848. inode = old;
  849. wait_on_inode(inode);
  850. }
  851. return inode;
  852. }
  853. /*
  854. * search the inode cache for a matching inode number.
  855. * If we find one, then the inode number we are trying to
  856. * allocate is not unique and so we should not use it.
  857. *
  858. * Returns 1 if the inode number is unique, 0 if it is not.
  859. */
  860. static int test_inode_iunique(struct super_block *sb, unsigned long ino)
  861. {
  862. struct hlist_head *b = inode_hashtable + hash(sb, ino);
  863. struct hlist_node *node;
  864. struct inode *inode;
  865. hlist_for_each_entry(inode, node, b, i_hash) {
  866. if (inode->i_ino == ino && inode->i_sb == sb)
  867. return 0;
  868. }
  869. return 1;
  870. }
  871. /**
  872. * iunique - get a unique inode number
  873. * @sb: superblock
  874. * @max_reserved: highest reserved inode number
  875. *
  876. * Obtain an inode number that is unique on the system for a given
  877. * superblock. This is used by file systems that have no natural
  878. * permanent inode numbering system. An inode number is returned that
  879. * is higher than the reserved limit but unique.
  880. *
  881. * BUGS:
  882. * With a large number of inodes live on the file system this function
  883. * currently becomes quite slow.
  884. */
  885. ino_t iunique(struct super_block *sb, ino_t max_reserved)
  886. {
  887. /*
  888. * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
  889. * error if st_ino won't fit in target struct field. Use 32bit counter
  890. * here to attempt to avoid that.
  891. */
  892. static DEFINE_SPINLOCK(iunique_lock);
  893. static unsigned int counter;
  894. ino_t res;
  895. spin_lock(&inode_lock);
  896. spin_lock(&iunique_lock);
  897. do {
  898. if (counter <= max_reserved)
  899. counter = max_reserved + 1;
  900. res = counter++;
  901. } while (!test_inode_iunique(sb, res));
  902. spin_unlock(&iunique_lock);
  903. spin_unlock(&inode_lock);
  904. return res;
  905. }
  906. EXPORT_SYMBOL(iunique);
  907. struct inode *igrab(struct inode *inode)
  908. {
  909. spin_lock(&inode_lock);
  910. if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
  911. __iget(inode);
  912. else
  913. /*
  914. * Handle the case where s_op->clear_inode is not been
  915. * called yet, and somebody is calling igrab
  916. * while the inode is getting freed.
  917. */
  918. inode = NULL;
  919. spin_unlock(&inode_lock);
  920. return inode;
  921. }
  922. EXPORT_SYMBOL(igrab);
  923. /**
  924. * ifind - internal function, you want ilookup5() or iget5().
  925. * @sb: super block of file system to search
  926. * @head: the head of the list to search
  927. * @test: callback used for comparisons between inodes
  928. * @data: opaque data pointer to pass to @test
  929. * @wait: if true wait for the inode to be unlocked, if false do not
  930. *
  931. * ifind() searches for the inode specified by @data in the inode
  932. * cache. This is a generalized version of ifind_fast() for file systems where
  933. * the inode number is not sufficient for unique identification of an inode.
  934. *
  935. * If the inode is in the cache, the inode is returned with an incremented
  936. * reference count.
  937. *
  938. * Otherwise NULL is returned.
  939. *
  940. * Note, @test is called with the inode_lock held, so can't sleep.
  941. */
  942. static struct inode *ifind(struct super_block *sb,
  943. struct hlist_head *head, int (*test)(struct inode *, void *),
  944. void *data, const int wait)
  945. {
  946. struct inode *inode;
  947. spin_lock(&inode_lock);
  948. inode = find_inode(sb, head, test, data);
  949. if (inode) {
  950. spin_unlock(&inode_lock);
  951. if (likely(wait))
  952. wait_on_inode(inode);
  953. return inode;
  954. }
  955. spin_unlock(&inode_lock);
  956. return NULL;
  957. }
  958. /**
  959. * ifind_fast - internal function, you want ilookup() or iget().
  960. * @sb: super block of file system to search
  961. * @head: head of the list to search
  962. * @ino: inode number to search for
  963. *
  964. * ifind_fast() searches for the inode @ino in the inode cache. This is for
  965. * file systems where the inode number is sufficient for unique identification
  966. * of an inode.
  967. *
  968. * If the inode is in the cache, the inode is returned with an incremented
  969. * reference count.
  970. *
  971. * Otherwise NULL is returned.
  972. */
  973. static struct inode *ifind_fast(struct super_block *sb,
  974. struct hlist_head *head, unsigned long ino)
  975. {
  976. struct inode *inode;
  977. spin_lock(&inode_lock);
  978. inode = find_inode_fast(sb, head, ino);
  979. if (inode) {
  980. spin_unlock(&inode_lock);
  981. wait_on_inode(inode);
  982. return inode;
  983. }
  984. spin_unlock(&inode_lock);
  985. return NULL;
  986. }
  987. /**
  988. * ilookup5_nowait - search for an inode in the inode cache
  989. * @sb: super block of file system to search
  990. * @hashval: hash value (usually inode number) to search for
  991. * @test: callback used for comparisons between inodes
  992. * @data: opaque data pointer to pass to @test
  993. *
  994. * ilookup5() uses ifind() to search for the inode specified by @hashval and
  995. * @data in the inode cache. This is a generalized version of ilookup() for
  996. * file systems where the inode number is not sufficient for unique
  997. * identification of an inode.
  998. *
  999. * If the inode is in the cache, the inode is returned with an incremented
  1000. * reference count. Note, the inode lock is not waited upon so you have to be
  1001. * very careful what you do with the returned inode. You probably should be
  1002. * using ilookup5() instead.
  1003. *
  1004. * Otherwise NULL is returned.
  1005. *
  1006. * Note, @test is called with the inode_lock held, so can't sleep.
  1007. */
  1008. struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
  1009. int (*test)(struct inode *, void *), void *data)
  1010. {
  1011. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1012. return ifind(sb, head, test, data, 0);
  1013. }
  1014. EXPORT_SYMBOL(ilookup5_nowait);
  1015. /**
  1016. * ilookup5 - search for an inode in the inode cache
  1017. * @sb: super block of file system to search
  1018. * @hashval: hash value (usually inode number) to search for
  1019. * @test: callback used for comparisons between inodes
  1020. * @data: opaque data pointer to pass to @test
  1021. *
  1022. * ilookup5() uses ifind() to search for the inode specified by @hashval and
  1023. * @data in the inode cache. This is a generalized version of ilookup() for
  1024. * file systems where the inode number is not sufficient for unique
  1025. * identification of an inode.
  1026. *
  1027. * If the inode is in the cache, the inode lock is waited upon and the inode is
  1028. * returned with an incremented reference count.
  1029. *
  1030. * Otherwise NULL is returned.
  1031. *
  1032. * Note, @test is called with the inode_lock held, so can't sleep.
  1033. */
  1034. struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
  1035. int (*test)(struct inode *, void *), void *data)
  1036. {
  1037. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1038. return ifind(sb, head, test, data, 1);
  1039. }
  1040. EXPORT_SYMBOL(ilookup5);
  1041. /**
  1042. * ilookup - search for an inode in the inode cache
  1043. * @sb: super block of file system to search
  1044. * @ino: inode number to search for
  1045. *
  1046. * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
  1047. * This is for file systems where the inode number is sufficient for unique
  1048. * identification of an inode.
  1049. *
  1050. * If the inode is in the cache, the inode is returned with an incremented
  1051. * reference count.
  1052. *
  1053. * Otherwise NULL is returned.
  1054. */
  1055. struct inode *ilookup(struct super_block *sb, unsigned long ino)
  1056. {
  1057. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1058. return ifind_fast(sb, head, ino);
  1059. }
  1060. EXPORT_SYMBOL(ilookup);
  1061. /**
  1062. * iget5_locked - obtain an inode from a mounted file system
  1063. * @sb: super block of file system
  1064. * @hashval: hash value (usually inode number) to get
  1065. * @test: callback used for comparisons between inodes
  1066. * @set: callback used to initialize a new struct inode
  1067. * @data: opaque data pointer to pass to @test and @set
  1068. *
  1069. * iget5_locked() uses ifind() to search for the inode specified by @hashval
  1070. * and @data in the inode cache and if present it is returned with an increased
  1071. * reference count. This is a generalized version of iget_locked() for file
  1072. * systems where the inode number is not sufficient for unique identification
  1073. * of an inode.
  1074. *
  1075. * If the inode is not in cache, get_new_inode() is called to allocate a new
  1076. * inode and this is returned locked, hashed, and with the I_NEW flag set. The
  1077. * file system gets to fill it in before unlocking it via unlock_new_inode().
  1078. *
  1079. * Note both @test and @set are called with the inode_lock held, so can't sleep.
  1080. */
  1081. struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
  1082. int (*test)(struct inode *, void *),
  1083. int (*set)(struct inode *, void *), void *data)
  1084. {
  1085. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1086. struct inode *inode;
  1087. inode = ifind(sb, head, test, data, 1);
  1088. if (inode)
  1089. return inode;
  1090. /*
  1091. * get_new_inode() will do the right thing, re-trying the search
  1092. * in case it had to block at any point.
  1093. */
  1094. return get_new_inode(sb, head, test, set, data);
  1095. }
  1096. EXPORT_SYMBOL(iget5_locked);
  1097. /**
  1098. * iget_locked - obtain an inode from a mounted file system
  1099. * @sb: super block of file system
  1100. * @ino: inode number to get
  1101. *
  1102. * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
  1103. * the inode cache and if present it is returned with an increased reference
  1104. * count. This is for file systems where the inode number is sufficient for
  1105. * unique identification of an inode.
  1106. *
  1107. * If the inode is not in cache, get_new_inode_fast() is called to allocate a
  1108. * new inode and this is returned locked, hashed, and with the I_NEW flag set.
  1109. * The file system gets to fill it in before unlocking it via
  1110. * unlock_new_inode().
  1111. */
  1112. struct inode *iget_locked(struct super_block *sb, unsigned long ino)
  1113. {
  1114. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1115. struct inode *inode;
  1116. inode = ifind_fast(sb, head, ino);
  1117. if (inode)
  1118. return inode;
  1119. /*
  1120. * get_new_inode_fast() will do the right thing, re-trying the search
  1121. * in case it had to block at any point.
  1122. */
  1123. return get_new_inode_fast(sb, head, ino);
  1124. }
  1125. EXPORT_SYMBOL(iget_locked);
  1126. int insert_inode_locked(struct inode *inode)
  1127. {
  1128. struct super_block *sb = inode->i_sb;
  1129. ino_t ino = inode->i_ino;
  1130. struct hlist_head *head = inode_hashtable + hash(sb, ino);
  1131. inode->i_state |= I_NEW;
  1132. while (1) {
  1133. struct hlist_node *node;
  1134. struct inode *old = NULL;
  1135. spin_lock(&inode_lock);
  1136. hlist_for_each_entry(old, node, head, i_hash) {
  1137. if (old->i_ino != ino)
  1138. continue;
  1139. if (old->i_sb != sb)
  1140. continue;
  1141. if (old->i_state & (I_FREEING|I_WILL_FREE))
  1142. continue;
  1143. break;
  1144. }
  1145. if (likely(!node)) {
  1146. hlist_add_head(&inode->i_hash, head);
  1147. spin_unlock(&inode_lock);
  1148. return 0;
  1149. }
  1150. __iget(old);
  1151. spin_unlock(&inode_lock);
  1152. wait_on_inode(old);
  1153. if (unlikely(!inode_unhashed(old))) {
  1154. iput(old);
  1155. return -EBUSY;
  1156. }
  1157. iput(old);
  1158. }
  1159. }
  1160. EXPORT_SYMBOL(insert_inode_locked);
  1161. int insert_inode_locked4(struct inode *inode, unsigned long hashval,
  1162. int (*test)(struct inode *, void *), void *data)
  1163. {
  1164. struct super_block *sb = inode->i_sb;
  1165. struct hlist_head *head = inode_hashtable + hash(sb, hashval);
  1166. inode->i_state |= I_NEW;
  1167. while (1) {
  1168. struct hlist_node *node;
  1169. struct inode *old = NULL;
  1170. spin_lock(&inode_lock);
  1171. hlist_for_each_entry(old, node, head, i_hash) {
  1172. if (old->i_sb != sb)
  1173. continue;
  1174. if (!test(old, data))
  1175. continue;
  1176. if (old->i_state & (I_FREEING|I_WILL_FREE))
  1177. continue;
  1178. break;
  1179. }
  1180. if (likely(!node)) {
  1181. hlist_add_head(&inode->i_hash, head);
  1182. spin_unlock(&inode_lock);
  1183. return 0;
  1184. }
  1185. __iget(old);
  1186. spin_unlock(&inode_lock);
  1187. wait_on_inode(old);
  1188. if (unlikely(!inode_unhashed(old))) {
  1189. iput(old);
  1190. return -EBUSY;
  1191. }
  1192. iput(old);
  1193. }
  1194. }
  1195. EXPORT_SYMBOL(insert_inode_locked4);
  1196. int generic_delete_inode(struct inode *inode)
  1197. {
  1198. return 1;
  1199. }
  1200. EXPORT_SYMBOL(generic_delete_inode);
  1201. /*
  1202. * Normal UNIX filesystem behaviour: delete the
  1203. * inode when the usage count drops to zero, and
  1204. * i_nlink is zero.
  1205. */
  1206. int generic_drop_inode(struct inode *inode)
  1207. {
  1208. return !inode->i_nlink || inode_unhashed(inode);
  1209. }
  1210. EXPORT_SYMBOL_GPL(generic_drop_inode);
  1211. /*
  1212. * Called when we're dropping the last reference
  1213. * to an inode.
  1214. *
  1215. * Call the FS "drop_inode()" function, defaulting to
  1216. * the legacy UNIX filesystem behaviour. If it tells
  1217. * us to evict inode, do so. Otherwise, retain inode
  1218. * in cache if fs is alive, sync and evict if fs is
  1219. * shutting down.
  1220. */
  1221. static void iput_final(struct inode *inode)
  1222. {
  1223. struct super_block *sb = inode->i_sb;
  1224. const struct super_operations *op = inode->i_sb->s_op;
  1225. int drop;
  1226. if (op && op->drop_inode)
  1227. drop = op->drop_inode(inode);
  1228. else
  1229. drop = generic_drop_inode(inode);
  1230. if (!drop) {
  1231. if (sb->s_flags & MS_ACTIVE) {
  1232. inode->i_state |= I_REFERENCED;
  1233. if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
  1234. inode_lru_list_add(inode);
  1235. }
  1236. spin_unlock(&inode_lock);
  1237. return;
  1238. }
  1239. WARN_ON(inode->i_state & I_NEW);
  1240. inode->i_state |= I_WILL_FREE;
  1241. spin_unlock(&inode_lock);
  1242. write_inode_now(inode, 1);
  1243. spin_lock(&inode_lock);
  1244. WARN_ON(inode->i_state & I_NEW);
  1245. inode->i_state &= ~I_WILL_FREE;
  1246. __remove_inode_hash(inode);
  1247. }
  1248. WARN_ON(inode->i_state & I_NEW);
  1249. inode->i_state |= I_FREEING;
  1250. /*
  1251. * Move the inode off the IO lists and LRU once I_FREEING is
  1252. * set so that it won't get moved back on there if it is dirty.
  1253. */
  1254. inode_lru_list_del(inode);
  1255. list_del_init(&inode->i_wb_list);
  1256. __inode_sb_list_del(inode);
  1257. spin_unlock(&inode_lock);
  1258. evict(inode);
  1259. remove_inode_hash(inode);
  1260. wake_up_inode(inode);
  1261. BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
  1262. destroy_inode(inode);
  1263. }
  1264. /**
  1265. * iput - put an inode
  1266. * @inode: inode to put
  1267. *
  1268. * Puts an inode, dropping its usage count. If the inode use count hits
  1269. * zero, the inode is then freed and may also be destroyed.
  1270. *
  1271. * Consequently, iput() can sleep.
  1272. */
  1273. void iput(struct inode *inode)
  1274. {
  1275. if (inode) {
  1276. BUG_ON(inode->i_state & I_CLEAR);
  1277. if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
  1278. iput_final(inode);
  1279. }
  1280. }
  1281. EXPORT_SYMBOL(iput);
  1282. /**
  1283. * bmap - find a block number in a file
  1284. * @inode: inode of file
  1285. * @block: block to find
  1286. *
  1287. * Returns the block number on the device holding the inode that
  1288. * is the disk block number for the block of the file requested.
  1289. * That is, asked for block 4 of inode 1 the function will return the
  1290. * disk block relative to the disk start that holds that block of the
  1291. * file.
  1292. */
  1293. sector_t bmap(struct inode *inode, sector_t block)
  1294. {
  1295. sector_t res = 0;
  1296. if (inode->i_mapping->a_ops->bmap)
  1297. res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
  1298. return res;
  1299. }
  1300. EXPORT_SYMBOL(bmap);
  1301. /*
  1302. * With relative atime, only update atime if the previous atime is
  1303. * earlier than either the ctime or mtime or if at least a day has
  1304. * passed since the last atime update.
  1305. */
  1306. static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
  1307. struct timespec now)
  1308. {
  1309. if (!(mnt->mnt_flags & MNT_RELATIME))
  1310. return 1;
  1311. /*
  1312. * Is mtime younger than atime? If yes, update atime:
  1313. */
  1314. if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
  1315. return 1;
  1316. /*
  1317. * Is ctime younger than atime? If yes, update atime:
  1318. */
  1319. if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
  1320. return 1;
  1321. /*
  1322. * Is the previous atime value older than a day? If yes,
  1323. * update atime:
  1324. */
  1325. if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
  1326. return 1;
  1327. /*
  1328. * Good, we can skip the atime update:
  1329. */
  1330. return 0;
  1331. }
  1332. /**
  1333. * touch_atime - update the access time
  1334. * @mnt: mount the inode is accessed on
  1335. * @dentry: dentry accessed
  1336. *
  1337. * Update the accessed time on an inode and mark it for writeback.
  1338. * This function automatically handles read only file systems and media,
  1339. * as well as the "noatime" flag and inode specific "noatime" markers.
  1340. */
  1341. void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
  1342. {
  1343. struct inode *inode = dentry->d_inode;
  1344. struct timespec now;
  1345. if (inode->i_flags & S_NOATIME)
  1346. return;
  1347. if (IS_NOATIME(inode))
  1348. return;
  1349. if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
  1350. return;
  1351. if (mnt->mnt_flags & MNT_NOATIME)
  1352. return;
  1353. if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
  1354. return;
  1355. now = current_fs_time(inode->i_sb);
  1356. if (!relatime_need_update(mnt, inode, now))
  1357. return;
  1358. if (timespec_equal(&inode->i_atime, &now))
  1359. return;
  1360. if (mnt_want_write(mnt))
  1361. return;
  1362. inode->i_atime = now;
  1363. mark_inode_dirty_sync(inode);
  1364. mnt_drop_write(mnt);
  1365. }
  1366. EXPORT_SYMBOL(touch_atime);
  1367. /**
  1368. * file_update_time - update mtime and ctime time
  1369. * @file: file accessed
  1370. *
  1371. * Update the mtime and ctime members of an inode and mark the inode
  1372. * for writeback. Note that this function is meant exclusively for
  1373. * usage in the file write path of filesystems, and filesystems may
  1374. * choose to explicitly ignore update via this function with the
  1375. * S_NOCMTIME inode flag, e.g. for network filesystem where these
  1376. * timestamps are handled by the server.
  1377. */
  1378. void file_update_time(struct file *file)
  1379. {
  1380. struct inode *inode = file->f_path.dentry->d_inode;
  1381. struct timespec now;
  1382. enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
  1383. /* First try to exhaust all avenues to not sync */
  1384. if (IS_NOCMTIME(inode))
  1385. return;
  1386. now = current_fs_time(inode->i_sb);
  1387. if (!timespec_equal(&inode->i_mtime, &now))
  1388. sync_it = S_MTIME;
  1389. if (!timespec_equal(&inode->i_ctime, &now))
  1390. sync_it |= S_CTIME;
  1391. if (IS_I_VERSION(inode))
  1392. sync_it |= S_VERSION;
  1393. if (!sync_it)
  1394. return;
  1395. /* Finally allowed to write? Takes lock. */
  1396. if (mnt_want_write_file(file))
  1397. return;
  1398. /* Only change inode inside the lock region */
  1399. if (sync_it & S_VERSION)
  1400. inode_inc_iversion(inode);
  1401. if (sync_it & S_CTIME)
  1402. inode->i_ctime = now;
  1403. if (sync_it & S_MTIME)
  1404. inode->i_mtime = now;
  1405. mark_inode_dirty_sync(inode);
  1406. mnt_drop_write(file->f_path.mnt);
  1407. }
  1408. EXPORT_SYMBOL(file_update_time);
  1409. int inode_needs_sync(struct inode *inode)
  1410. {
  1411. if (IS_SYNC(inode))
  1412. return 1;
  1413. if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
  1414. return 1;
  1415. return 0;
  1416. }
  1417. EXPORT_SYMBOL(inode_needs_sync);
  1418. int inode_wait(void *word)
  1419. {
  1420. schedule();
  1421. return 0;
  1422. }
  1423. EXPORT_SYMBOL(inode_wait);
  1424. /*
  1425. * If we try to find an inode in the inode hash while it is being
  1426. * deleted, we have to wait until the filesystem completes its
  1427. * deletion before reporting that it isn't found. This function waits
  1428. * until the deletion _might_ have completed. Callers are responsible
  1429. * to recheck inode state.
  1430. *
  1431. * It doesn't matter if I_NEW is not set initially, a call to
  1432. * wake_up_inode() after removing from the hash list will DTRT.
  1433. *
  1434. * This is called with inode_lock held.
  1435. */
  1436. static void __wait_on_freeing_inode(struct inode *inode)
  1437. {
  1438. wait_queue_head_t *wq;
  1439. DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
  1440. wq = bit_waitqueue(&inode->i_state, __I_NEW);
  1441. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1442. spin_unlock(&inode_lock);
  1443. schedule();
  1444. finish_wait(wq, &wait.wait);
  1445. spin_lock(&inode_lock);
  1446. }
  1447. static __initdata unsigned long ihash_entries;
  1448. static int __init set_ihash_entries(char *str)
  1449. {
  1450. if (!str)
  1451. return 0;
  1452. ihash_entries = simple_strtoul(str, &str, 0);
  1453. return 1;
  1454. }
  1455. __setup("ihash_entries=", set_ihash_entries);
  1456. /*
  1457. * Initialize the waitqueues and inode hash table.
  1458. */
  1459. void __init inode_init_early(void)
  1460. {
  1461. int loop;
  1462. /* If hashes are distributed across NUMA nodes, defer
  1463. * hash allocation until vmalloc space is available.
  1464. */
  1465. if (hashdist)
  1466. return;
  1467. inode_hashtable =
  1468. alloc_large_system_hash("Inode-cache",
  1469. sizeof(struct hlist_head),
  1470. ihash_entries,
  1471. 14,
  1472. HASH_EARLY,
  1473. &i_hash_shift,
  1474. &i_hash_mask,
  1475. 0);
  1476. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1477. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1478. }
  1479. void __init inode_init(void)
  1480. {
  1481. int loop;
  1482. /* inode slab cache */
  1483. inode_cachep = kmem_cache_create("inode_cache",
  1484. sizeof(struct inode),
  1485. 0,
  1486. (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
  1487. SLAB_MEM_SPREAD),
  1488. init_once);
  1489. register_shrinker(&icache_shrinker);
  1490. /* Hash may have been set up in inode_init_early */
  1491. if (!hashdist)
  1492. return;
  1493. inode_hashtable =
  1494. alloc_large_system_hash("Inode-cache",
  1495. sizeof(struct hlist_head),
  1496. ihash_entries,
  1497. 14,
  1498. 0,
  1499. &i_hash_shift,
  1500. &i_hash_mask,
  1501. 0);
  1502. for (loop = 0; loop < (1 << i_hash_shift); loop++)
  1503. INIT_HLIST_HEAD(&inode_hashtable[loop]);
  1504. }
  1505. void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
  1506. {
  1507. inode->i_mode = mode;
  1508. if (S_ISCHR(mode)) {
  1509. inode->i_fop = &def_chr_fops;
  1510. inode->i_rdev = rdev;
  1511. } else if (S_ISBLK(mode)) {
  1512. inode->i_fop = &def_blk_fops;
  1513. inode->i_rdev = rdev;
  1514. } else if (S_ISFIFO(mode))
  1515. inode->i_fop = &def_fifo_fops;
  1516. else if (S_ISSOCK(mode))
  1517. inode->i_fop = &bad_sock_fops;
  1518. else
  1519. printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
  1520. " inode %s:%lu\n", mode, inode->i_sb->s_id,
  1521. inode->i_ino);
  1522. }
  1523. EXPORT_SYMBOL(init_special_inode);
  1524. /**
  1525. * Init uid,gid,mode for new inode according to posix standards
  1526. * @inode: New inode
  1527. * @dir: Directory inode
  1528. * @mode: mode of the new inode
  1529. */
  1530. void inode_init_owner(struct inode *inode, const struct inode *dir,
  1531. mode_t mode)
  1532. {
  1533. inode->i_uid = current_fsuid();
  1534. if (dir && dir->i_mode & S_ISGID) {
  1535. inode->i_gid = dir->i_gid;
  1536. if (S_ISDIR(mode))
  1537. mode |= S_ISGID;
  1538. } else
  1539. inode->i_gid = current_fsgid();
  1540. inode->i_mode = mode;
  1541. }
  1542. EXPORT_SYMBOL(inode_init_owner);