inode.c 45 KB

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