inode.c 43 KB

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