inode.c 43 KB

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