inode.c 44 KB

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