inode.c 34 KB

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