inode.c 43 KB

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