inode.c 45 KB

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