inode.c 41 KB

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