inode.c 40 KB

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