inode.c 44 KB

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