inode.c 43 KB

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