inode.c 46 KB

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