inode.c 43 KB

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