inode.c 43 KB

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