inode.c 42 KB

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