inode.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. /*
  2. * linux/fs/nfs/inode.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs inode and superblock handling functions
  7. *
  8. * Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
  9. * experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10. *
  11. * Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12. * J.S.Peatfield@damtp.cam.ac.uk
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/time.h>
  19. #include <linux/kernel.h>
  20. #include <linux/mm.h>
  21. #include <linux/string.h>
  22. #include <linux/stat.h>
  23. #include <linux/errno.h>
  24. #include <linux/unistd.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/sunrpc/stats.h>
  27. #include <linux/sunrpc/metrics.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/nfs4_mount.h>
  31. #include <linux/lockd/bind.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/mount.h>
  34. #include <linux/nfs_idmap.h>
  35. #include <linux/vfs.h>
  36. #include <linux/inet.h>
  37. #include <linux/nfs_xdr.h>
  38. #include <linux/slab.h>
  39. #include <linux/compat.h>
  40. #include <linux/freezer.h>
  41. #include <linux/crc32.h>
  42. #include <asm/uaccess.h>
  43. #include "nfs4_fs.h"
  44. #include "callback.h"
  45. #include "delegation.h"
  46. #include "iostat.h"
  47. #include "internal.h"
  48. #include "fscache.h"
  49. #include "dns_resolve.h"
  50. #include "pnfs.h"
  51. #include "netns.h"
  52. #define NFSDBG_FACILITY NFSDBG_VFS
  53. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  54. /* Default is to see 64-bit inode numbers */
  55. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  56. static void nfs_invalidate_inode(struct inode *);
  57. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  58. static struct kmem_cache * nfs_inode_cachep;
  59. static inline unsigned long
  60. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  61. {
  62. return nfs_fileid_to_ino_t(fattr->fileid);
  63. }
  64. /**
  65. * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
  66. * @word: long word containing the bit lock
  67. */
  68. int nfs_wait_bit_killable(void *word)
  69. {
  70. if (fatal_signal_pending(current))
  71. return -ERESTARTSYS;
  72. freezable_schedule();
  73. return 0;
  74. }
  75. /**
  76. * nfs_compat_user_ino64 - returns the user-visible inode number
  77. * @fileid: 64-bit fileid
  78. *
  79. * This function returns a 32-bit inode number if the boot parameter
  80. * nfs.enable_ino64 is zero.
  81. */
  82. u64 nfs_compat_user_ino64(u64 fileid)
  83. {
  84. #ifdef CONFIG_COMPAT
  85. compat_ulong_t ino;
  86. #else
  87. unsigned long ino;
  88. #endif
  89. if (enable_ino64)
  90. return fileid;
  91. ino = fileid;
  92. if (sizeof(ino) < sizeof(fileid))
  93. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  94. return ino;
  95. }
  96. static void nfs_clear_inode(struct inode *inode)
  97. {
  98. /*
  99. * The following should never happen...
  100. */
  101. BUG_ON(nfs_have_writebacks(inode));
  102. BUG_ON(!list_empty(&NFS_I(inode)->open_files));
  103. nfs_zap_acl_cache(inode);
  104. nfs_access_zap_cache(inode);
  105. nfs_fscache_release_inode_cookie(inode);
  106. }
  107. void nfs_evict_inode(struct inode *inode)
  108. {
  109. truncate_inode_pages(&inode->i_data, 0);
  110. end_writeback(inode);
  111. nfs_clear_inode(inode);
  112. }
  113. /**
  114. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  115. */
  116. int nfs_sync_mapping(struct address_space *mapping)
  117. {
  118. int ret = 0;
  119. if (mapping->nrpages != 0) {
  120. unmap_mapping_range(mapping, 0, 0, 0);
  121. ret = nfs_wb_all(mapping->host);
  122. }
  123. return ret;
  124. }
  125. /*
  126. * Invalidate the local caches
  127. */
  128. static void nfs_zap_caches_locked(struct inode *inode)
  129. {
  130. struct nfs_inode *nfsi = NFS_I(inode);
  131. int mode = inode->i_mode;
  132. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  133. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  134. nfsi->attrtimeo_timestamp = jiffies;
  135. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  136. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  137. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  138. else
  139. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  140. }
  141. void nfs_zap_caches(struct inode *inode)
  142. {
  143. spin_lock(&inode->i_lock);
  144. nfs_zap_caches_locked(inode);
  145. spin_unlock(&inode->i_lock);
  146. }
  147. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  148. {
  149. if (mapping->nrpages != 0) {
  150. spin_lock(&inode->i_lock);
  151. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  152. spin_unlock(&inode->i_lock);
  153. }
  154. }
  155. void nfs_zap_acl_cache(struct inode *inode)
  156. {
  157. void (*clear_acl_cache)(struct inode *);
  158. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  159. if (clear_acl_cache != NULL)
  160. clear_acl_cache(inode);
  161. spin_lock(&inode->i_lock);
  162. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  163. spin_unlock(&inode->i_lock);
  164. }
  165. void nfs_invalidate_atime(struct inode *inode)
  166. {
  167. spin_lock(&inode->i_lock);
  168. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  169. spin_unlock(&inode->i_lock);
  170. }
  171. /*
  172. * Invalidate, but do not unhash, the inode.
  173. * NB: must be called with inode->i_lock held!
  174. */
  175. static void nfs_invalidate_inode(struct inode *inode)
  176. {
  177. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  178. nfs_zap_caches_locked(inode);
  179. }
  180. struct nfs_find_desc {
  181. struct nfs_fh *fh;
  182. struct nfs_fattr *fattr;
  183. };
  184. /*
  185. * In NFSv3 we can have 64bit inode numbers. In order to support
  186. * this, and re-exported directories (also seen in NFSv2)
  187. * we are forced to allow 2 different inodes to have the same
  188. * i_ino.
  189. */
  190. static int
  191. nfs_find_actor(struct inode *inode, void *opaque)
  192. {
  193. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  194. struct nfs_fh *fh = desc->fh;
  195. struct nfs_fattr *fattr = desc->fattr;
  196. if (NFS_FILEID(inode) != fattr->fileid)
  197. return 0;
  198. if (nfs_compare_fh(NFS_FH(inode), fh))
  199. return 0;
  200. if (is_bad_inode(inode) || NFS_STALE(inode))
  201. return 0;
  202. return 1;
  203. }
  204. static int
  205. nfs_init_locked(struct inode *inode, void *opaque)
  206. {
  207. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  208. struct nfs_fattr *fattr = desc->fattr;
  209. set_nfs_fileid(inode, fattr->fileid);
  210. nfs_copy_fh(NFS_FH(inode), desc->fh);
  211. return 0;
  212. }
  213. /*
  214. * This is our front-end to iget that looks up inodes by file handle
  215. * instead of inode number.
  216. */
  217. struct inode *
  218. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  219. {
  220. struct nfs_find_desc desc = {
  221. .fh = fh,
  222. .fattr = fattr
  223. };
  224. struct inode *inode = ERR_PTR(-ENOENT);
  225. unsigned long hash;
  226. nfs_attr_check_mountpoint(sb, fattr);
  227. if (((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) &&
  228. !nfs_attr_use_mounted_on_fileid(fattr))
  229. goto out_no_inode;
  230. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  231. goto out_no_inode;
  232. hash = nfs_fattr_to_ino_t(fattr);
  233. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  234. if (inode == NULL) {
  235. inode = ERR_PTR(-ENOMEM);
  236. goto out_no_inode;
  237. }
  238. if (inode->i_state & I_NEW) {
  239. struct nfs_inode *nfsi = NFS_I(inode);
  240. unsigned long now = jiffies;
  241. /* We set i_ino for the few things that still rely on it,
  242. * such as stat(2) */
  243. inode->i_ino = hash;
  244. /* We can't support update_atime(), since the server will reset it */
  245. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  246. inode->i_mode = fattr->mode;
  247. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  248. && nfs_server_capable(inode, NFS_CAP_MODE))
  249. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  250. /* Why so? Because we want revalidate for devices/FIFOs, and
  251. * that's precisely what we have in nfs_file_inode_operations.
  252. */
  253. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  254. if (S_ISREG(inode->i_mode)) {
  255. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  256. inode->i_data.a_ops = &nfs_file_aops;
  257. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  258. } else if (S_ISDIR(inode->i_mode)) {
  259. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  260. inode->i_fop = &nfs_dir_operations;
  261. inode->i_data.a_ops = &nfs_dir_aops;
  262. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS))
  263. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  264. /* Deal with crossing mountpoints */
  265. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  266. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  267. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  268. inode->i_op = &nfs_referral_inode_operations;
  269. else
  270. inode->i_op = &nfs_mountpoint_inode_operations;
  271. inode->i_fop = NULL;
  272. inode->i_flags |= S_AUTOMOUNT;
  273. }
  274. } else if (S_ISLNK(inode->i_mode))
  275. inode->i_op = &nfs_symlink_inode_operations;
  276. else
  277. init_special_inode(inode, inode->i_mode, fattr->rdev);
  278. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  279. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  280. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  281. inode->i_version = 0;
  282. inode->i_size = 0;
  283. clear_nlink(inode);
  284. inode->i_uid = -2;
  285. inode->i_gid = -2;
  286. inode->i_blocks = 0;
  287. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  288. nfsi->read_cache_jiffies = fattr->time_start;
  289. nfsi->attr_gencount = fattr->gencount;
  290. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  291. inode->i_atime = fattr->atime;
  292. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  293. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  294. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  295. inode->i_mtime = fattr->mtime;
  296. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  297. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  298. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  299. inode->i_ctime = fattr->ctime;
  300. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  301. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  302. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  303. inode->i_version = fattr->change_attr;
  304. else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
  305. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  306. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  307. inode->i_size = nfs_size_to_loff_t(fattr->size);
  308. else
  309. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  310. | NFS_INO_REVAL_PAGECACHE;
  311. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  312. set_nlink(inode, fattr->nlink);
  313. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  314. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  315. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  316. inode->i_uid = fattr->uid;
  317. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  318. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  319. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  320. inode->i_gid = fattr->gid;
  321. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  322. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  323. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  324. inode->i_blocks = fattr->du.nfs2.blocks;
  325. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  326. /*
  327. * report the blocks in 512byte units
  328. */
  329. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  330. }
  331. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  332. nfsi->attrtimeo_timestamp = now;
  333. nfsi->access_cache = RB_ROOT;
  334. nfs_fscache_init_inode_cookie(inode);
  335. unlock_new_inode(inode);
  336. } else
  337. nfs_refresh_inode(inode, fattr);
  338. dprintk("NFS: nfs_fhget(%s/%Ld fh_crc=0x%08x ct=%d)\n",
  339. inode->i_sb->s_id,
  340. (long long)NFS_FILEID(inode),
  341. nfs_display_fhandle_hash(fh),
  342. atomic_read(&inode->i_count));
  343. out:
  344. return inode;
  345. out_no_inode:
  346. dprintk("nfs_fhget: iget failed with error %ld\n", PTR_ERR(inode));
  347. goto out;
  348. }
  349. #define NFS_VALID_ATTRS (ATTR_MODE|ATTR_UID|ATTR_GID|ATTR_SIZE|ATTR_ATIME|ATTR_ATIME_SET|ATTR_MTIME|ATTR_MTIME_SET|ATTR_FILE|ATTR_OPEN)
  350. int
  351. nfs_setattr(struct dentry *dentry, struct iattr *attr)
  352. {
  353. struct inode *inode = dentry->d_inode;
  354. struct nfs_fattr *fattr;
  355. int error = -ENOMEM;
  356. nfs_inc_stats(inode, NFSIOS_VFSSETATTR);
  357. /* skip mode change if it's just for clearing setuid/setgid */
  358. if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  359. attr->ia_valid &= ~ATTR_MODE;
  360. if (attr->ia_valid & ATTR_SIZE) {
  361. if (!S_ISREG(inode->i_mode) || attr->ia_size == i_size_read(inode))
  362. attr->ia_valid &= ~ATTR_SIZE;
  363. }
  364. /* Optimization: if the end result is no change, don't RPC */
  365. attr->ia_valid &= NFS_VALID_ATTRS;
  366. if ((attr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
  367. return 0;
  368. /* Write all dirty data */
  369. if (S_ISREG(inode->i_mode))
  370. nfs_wb_all(inode);
  371. fattr = nfs_alloc_fattr();
  372. if (fattr == NULL)
  373. goto out;
  374. /*
  375. * Return any delegations if we're going to change ACLs
  376. */
  377. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
  378. nfs_inode_return_delegation(inode);
  379. error = NFS_PROTO(inode)->setattr(dentry, fattr, attr);
  380. if (error == 0)
  381. nfs_refresh_inode(inode, fattr);
  382. nfs_free_fattr(fattr);
  383. out:
  384. return error;
  385. }
  386. /**
  387. * nfs_vmtruncate - unmap mappings "freed" by truncate() syscall
  388. * @inode: inode of the file used
  389. * @offset: file offset to start truncating
  390. *
  391. * This is a copy of the common vmtruncate, but with the locking
  392. * corrected to take into account the fact that NFS requires
  393. * inode->i_size to be updated under the inode->i_lock.
  394. */
  395. static int nfs_vmtruncate(struct inode * inode, loff_t offset)
  396. {
  397. loff_t oldsize;
  398. int err;
  399. err = inode_newsize_ok(inode, offset);
  400. if (err)
  401. goto out;
  402. spin_lock(&inode->i_lock);
  403. oldsize = inode->i_size;
  404. i_size_write(inode, offset);
  405. spin_unlock(&inode->i_lock);
  406. truncate_pagecache(inode, oldsize, offset);
  407. out:
  408. return err;
  409. }
  410. /**
  411. * nfs_setattr_update_inode - Update inode metadata after a setattr call.
  412. * @inode: pointer to struct inode
  413. * @attr: pointer to struct iattr
  414. *
  415. * Note: we do this in the *proc.c in order to ensure that
  416. * it works for things like exclusive creates too.
  417. */
  418. void nfs_setattr_update_inode(struct inode *inode, struct iattr *attr)
  419. {
  420. if ((attr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0) {
  421. spin_lock(&inode->i_lock);
  422. if ((attr->ia_valid & ATTR_MODE) != 0) {
  423. int mode = attr->ia_mode & S_IALLUGO;
  424. mode |= inode->i_mode & ~S_IALLUGO;
  425. inode->i_mode = mode;
  426. }
  427. if ((attr->ia_valid & ATTR_UID) != 0)
  428. inode->i_uid = attr->ia_uid;
  429. if ((attr->ia_valid & ATTR_GID) != 0)
  430. inode->i_gid = attr->ia_gid;
  431. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  432. spin_unlock(&inode->i_lock);
  433. }
  434. if ((attr->ia_valid & ATTR_SIZE) != 0) {
  435. nfs_inc_stats(inode, NFSIOS_SETATTRTRUNC);
  436. nfs_vmtruncate(inode, attr->ia_size);
  437. }
  438. }
  439. int nfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat)
  440. {
  441. struct inode *inode = dentry->d_inode;
  442. int need_atime = NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATIME;
  443. int err;
  444. /* Flush out writes to the server in order to update c/mtime. */
  445. if (S_ISREG(inode->i_mode)) {
  446. err = filemap_write_and_wait(inode->i_mapping);
  447. if (err)
  448. goto out;
  449. }
  450. /*
  451. * We may force a getattr if the user cares about atime.
  452. *
  453. * Note that we only have to check the vfsmount flags here:
  454. * - NFS always sets S_NOATIME by so checking it would give a
  455. * bogus result
  456. * - NFS never sets MS_NOATIME or MS_NODIRATIME so there is
  457. * no point in checking those.
  458. */
  459. if ((mnt->mnt_flags & MNT_NOATIME) ||
  460. ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))
  461. need_atime = 0;
  462. if (need_atime)
  463. err = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  464. else
  465. err = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  466. if (!err) {
  467. generic_fillattr(inode, stat);
  468. stat->ino = nfs_compat_user_ino64(NFS_FILEID(inode));
  469. }
  470. out:
  471. return err;
  472. }
  473. static void nfs_init_lock_context(struct nfs_lock_context *l_ctx)
  474. {
  475. atomic_set(&l_ctx->count, 1);
  476. l_ctx->lockowner = current->files;
  477. l_ctx->pid = current->tgid;
  478. INIT_LIST_HEAD(&l_ctx->list);
  479. }
  480. static struct nfs_lock_context *__nfs_find_lock_context(struct nfs_open_context *ctx)
  481. {
  482. struct nfs_lock_context *pos;
  483. list_for_each_entry(pos, &ctx->lock_context.list, list) {
  484. if (pos->lockowner != current->files)
  485. continue;
  486. if (pos->pid != current->tgid)
  487. continue;
  488. atomic_inc(&pos->count);
  489. return pos;
  490. }
  491. return NULL;
  492. }
  493. struct nfs_lock_context *nfs_get_lock_context(struct nfs_open_context *ctx)
  494. {
  495. struct nfs_lock_context *res, *new = NULL;
  496. struct inode *inode = ctx->dentry->d_inode;
  497. spin_lock(&inode->i_lock);
  498. res = __nfs_find_lock_context(ctx);
  499. if (res == NULL) {
  500. spin_unlock(&inode->i_lock);
  501. new = kmalloc(sizeof(*new), GFP_KERNEL);
  502. if (new == NULL)
  503. return NULL;
  504. nfs_init_lock_context(new);
  505. spin_lock(&inode->i_lock);
  506. res = __nfs_find_lock_context(ctx);
  507. if (res == NULL) {
  508. list_add_tail(&new->list, &ctx->lock_context.list);
  509. new->open_context = ctx;
  510. res = new;
  511. new = NULL;
  512. }
  513. }
  514. spin_unlock(&inode->i_lock);
  515. kfree(new);
  516. return res;
  517. }
  518. void nfs_put_lock_context(struct nfs_lock_context *l_ctx)
  519. {
  520. struct nfs_open_context *ctx = l_ctx->open_context;
  521. struct inode *inode = ctx->dentry->d_inode;
  522. if (!atomic_dec_and_lock(&l_ctx->count, &inode->i_lock))
  523. return;
  524. list_del(&l_ctx->list);
  525. spin_unlock(&inode->i_lock);
  526. kfree(l_ctx);
  527. }
  528. /**
  529. * nfs_close_context - Common close_context() routine NFSv2/v3
  530. * @ctx: pointer to context
  531. * @is_sync: is this a synchronous close
  532. *
  533. * always ensure that the attributes are up to date if we're mounted
  534. * with close-to-open semantics
  535. */
  536. void nfs_close_context(struct nfs_open_context *ctx, int is_sync)
  537. {
  538. struct inode *inode;
  539. struct nfs_server *server;
  540. if (!(ctx->mode & FMODE_WRITE))
  541. return;
  542. if (!is_sync)
  543. return;
  544. inode = ctx->dentry->d_inode;
  545. if (!list_empty(&NFS_I(inode)->open_files))
  546. return;
  547. server = NFS_SERVER(inode);
  548. if (server->flags & NFS_MOUNT_NOCTO)
  549. return;
  550. nfs_revalidate_inode(server, inode);
  551. }
  552. struct nfs_open_context *alloc_nfs_open_context(struct dentry *dentry, fmode_t f_mode)
  553. {
  554. struct nfs_open_context *ctx;
  555. struct rpc_cred *cred = rpc_lookup_cred();
  556. if (IS_ERR(cred))
  557. return ERR_CAST(cred);
  558. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  559. if (!ctx) {
  560. put_rpccred(cred);
  561. return ERR_PTR(-ENOMEM);
  562. }
  563. nfs_sb_active(dentry->d_sb);
  564. ctx->dentry = dget(dentry);
  565. ctx->cred = cred;
  566. ctx->state = NULL;
  567. ctx->mode = f_mode;
  568. ctx->flags = 0;
  569. ctx->error = 0;
  570. nfs_init_lock_context(&ctx->lock_context);
  571. ctx->lock_context.open_context = ctx;
  572. INIT_LIST_HEAD(&ctx->list);
  573. return ctx;
  574. }
  575. struct nfs_open_context *get_nfs_open_context(struct nfs_open_context *ctx)
  576. {
  577. if (ctx != NULL)
  578. atomic_inc(&ctx->lock_context.count);
  579. return ctx;
  580. }
  581. static void __put_nfs_open_context(struct nfs_open_context *ctx, int is_sync)
  582. {
  583. struct inode *inode = ctx->dentry->d_inode;
  584. struct super_block *sb = ctx->dentry->d_sb;
  585. if (!list_empty(&ctx->list)) {
  586. if (!atomic_dec_and_lock(&ctx->lock_context.count, &inode->i_lock))
  587. return;
  588. list_del(&ctx->list);
  589. spin_unlock(&inode->i_lock);
  590. } else if (!atomic_dec_and_test(&ctx->lock_context.count))
  591. return;
  592. if (inode != NULL)
  593. NFS_PROTO(inode)->close_context(ctx, is_sync);
  594. if (ctx->cred != NULL)
  595. put_rpccred(ctx->cred);
  596. dput(ctx->dentry);
  597. nfs_sb_deactive(sb);
  598. kfree(ctx);
  599. }
  600. void put_nfs_open_context(struct nfs_open_context *ctx)
  601. {
  602. __put_nfs_open_context(ctx, 0);
  603. }
  604. /*
  605. * Ensure that mmap has a recent RPC credential for use when writing out
  606. * shared pages
  607. */
  608. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  609. {
  610. struct inode *inode = filp->f_path.dentry->d_inode;
  611. struct nfs_inode *nfsi = NFS_I(inode);
  612. filp->private_data = get_nfs_open_context(ctx);
  613. spin_lock(&inode->i_lock);
  614. list_add(&ctx->list, &nfsi->open_files);
  615. spin_unlock(&inode->i_lock);
  616. }
  617. /*
  618. * Given an inode, search for an open context with the desired characteristics
  619. */
  620. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  621. {
  622. struct nfs_inode *nfsi = NFS_I(inode);
  623. struct nfs_open_context *pos, *ctx = NULL;
  624. spin_lock(&inode->i_lock);
  625. list_for_each_entry(pos, &nfsi->open_files, list) {
  626. if (cred != NULL && pos->cred != cred)
  627. continue;
  628. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  629. continue;
  630. ctx = get_nfs_open_context(pos);
  631. break;
  632. }
  633. spin_unlock(&inode->i_lock);
  634. return ctx;
  635. }
  636. static void nfs_file_clear_open_context(struct file *filp)
  637. {
  638. struct inode *inode = filp->f_path.dentry->d_inode;
  639. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  640. if (ctx) {
  641. filp->private_data = NULL;
  642. spin_lock(&inode->i_lock);
  643. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  644. spin_unlock(&inode->i_lock);
  645. __put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1);
  646. }
  647. }
  648. /*
  649. * These allocate and release file read/write context information.
  650. */
  651. int nfs_open(struct inode *inode, struct file *filp)
  652. {
  653. struct nfs_open_context *ctx;
  654. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  655. if (IS_ERR(ctx))
  656. return PTR_ERR(ctx);
  657. nfs_file_set_open_context(filp, ctx);
  658. put_nfs_open_context(ctx);
  659. nfs_fscache_set_inode_cookie(inode, filp);
  660. return 0;
  661. }
  662. int nfs_release(struct inode *inode, struct file *filp)
  663. {
  664. nfs_file_clear_open_context(filp);
  665. return 0;
  666. }
  667. /*
  668. * This function is called whenever some part of NFS notices that
  669. * the cached attributes have to be refreshed.
  670. */
  671. int
  672. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  673. {
  674. int status = -ESTALE;
  675. struct nfs_fattr *fattr = NULL;
  676. struct nfs_inode *nfsi = NFS_I(inode);
  677. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  678. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  679. if (is_bad_inode(inode))
  680. goto out;
  681. if (NFS_STALE(inode))
  682. goto out;
  683. status = -ENOMEM;
  684. fattr = nfs_alloc_fattr();
  685. if (fattr == NULL)
  686. goto out;
  687. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  688. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr);
  689. if (status != 0) {
  690. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  691. inode->i_sb->s_id,
  692. (long long)NFS_FILEID(inode), status);
  693. if (status == -ESTALE) {
  694. nfs_zap_caches(inode);
  695. if (!S_ISDIR(inode->i_mode))
  696. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  697. }
  698. goto out;
  699. }
  700. status = nfs_refresh_inode(inode, fattr);
  701. if (status) {
  702. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  703. inode->i_sb->s_id,
  704. (long long)NFS_FILEID(inode), status);
  705. goto out;
  706. }
  707. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  708. nfs_zap_acl_cache(inode);
  709. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  710. inode->i_sb->s_id,
  711. (long long)NFS_FILEID(inode));
  712. out:
  713. nfs_free_fattr(fattr);
  714. return status;
  715. }
  716. int nfs_attribute_timeout(struct inode *inode)
  717. {
  718. struct nfs_inode *nfsi = NFS_I(inode);
  719. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  720. }
  721. static int nfs_attribute_cache_expired(struct inode *inode)
  722. {
  723. if (nfs_have_delegated_attributes(inode))
  724. return 0;
  725. return nfs_attribute_timeout(inode);
  726. }
  727. /**
  728. * nfs_revalidate_inode - Revalidate the inode attributes
  729. * @server - pointer to nfs_server struct
  730. * @inode - pointer to inode struct
  731. *
  732. * Updates inode attribute information by retrieving the data from the server.
  733. */
  734. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  735. {
  736. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  737. && !nfs_attribute_cache_expired(inode))
  738. return NFS_STALE(inode) ? -ESTALE : 0;
  739. return __nfs_revalidate_inode(server, inode);
  740. }
  741. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  742. {
  743. struct nfs_inode *nfsi = NFS_I(inode);
  744. if (mapping->nrpages != 0) {
  745. int ret = invalidate_inode_pages2(mapping);
  746. if (ret < 0)
  747. return ret;
  748. }
  749. spin_lock(&inode->i_lock);
  750. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  751. if (S_ISDIR(inode->i_mode))
  752. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  753. spin_unlock(&inode->i_lock);
  754. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  755. nfs_fscache_reset_inode_cookie(inode);
  756. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  757. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  758. return 0;
  759. }
  760. static bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  761. {
  762. if (nfs_have_delegated_attributes(inode))
  763. return false;
  764. return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
  765. || nfs_attribute_timeout(inode)
  766. || NFS_STALE(inode);
  767. }
  768. /**
  769. * nfs_revalidate_mapping - Revalidate the pagecache
  770. * @inode - pointer to host inode
  771. * @mapping - pointer to mapping
  772. */
  773. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  774. {
  775. struct nfs_inode *nfsi = NFS_I(inode);
  776. int ret = 0;
  777. if (nfs_mapping_need_revalidate_inode(inode)) {
  778. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  779. if (ret < 0)
  780. goto out;
  781. }
  782. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  783. ret = nfs_invalidate_mapping(inode, mapping);
  784. out:
  785. return ret;
  786. }
  787. static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  788. {
  789. struct nfs_inode *nfsi = NFS_I(inode);
  790. unsigned long ret = 0;
  791. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  792. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  793. && inode->i_version == fattr->pre_change_attr) {
  794. inode->i_version = fattr->change_attr;
  795. if (S_ISDIR(inode->i_mode))
  796. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  797. ret |= NFS_INO_INVALID_ATTR;
  798. }
  799. /* If we have atomic WCC data, we may update some attributes */
  800. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  801. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  802. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  803. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  804. ret |= NFS_INO_INVALID_ATTR;
  805. }
  806. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  807. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  808. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  809. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  810. if (S_ISDIR(inode->i_mode))
  811. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  812. ret |= NFS_INO_INVALID_ATTR;
  813. }
  814. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  815. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  816. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  817. && nfsi->npages == 0) {
  818. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  819. ret |= NFS_INO_INVALID_ATTR;
  820. }
  821. return ret;
  822. }
  823. /**
  824. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  825. * @inode - pointer to inode
  826. * @fattr - updated attributes
  827. *
  828. * Verifies the attribute cache. If we have just changed the attributes,
  829. * so that fattr carries weak cache consistency data, then it may
  830. * also update the ctime/mtime/change_attribute.
  831. */
  832. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  833. {
  834. struct nfs_inode *nfsi = NFS_I(inode);
  835. loff_t cur_size, new_isize;
  836. unsigned long invalid = 0;
  837. if (nfs_have_delegated_attributes(inode))
  838. return 0;
  839. /* Has the inode gone and changed behind our back? */
  840. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  841. return -EIO;
  842. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  843. return -EIO;
  844. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  845. inode->i_version != fattr->change_attr)
  846. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  847. /* Verify a few of the more important attributes */
  848. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  849. invalid |= NFS_INO_INVALID_ATTR;
  850. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  851. cur_size = i_size_read(inode);
  852. new_isize = nfs_size_to_loff_t(fattr->size);
  853. if (cur_size != new_isize && nfsi->npages == 0)
  854. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  855. }
  856. /* Have any file permissions changed? */
  857. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  858. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  859. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
  860. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  861. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
  862. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  863. /* Has the link count changed? */
  864. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  865. invalid |= NFS_INO_INVALID_ATTR;
  866. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  867. invalid |= NFS_INO_INVALID_ATIME;
  868. if (invalid != 0)
  869. nfsi->cache_validity |= invalid;
  870. nfsi->read_cache_jiffies = fattr->time_start;
  871. return 0;
  872. }
  873. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  874. {
  875. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  876. return 0;
  877. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  878. }
  879. static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  880. {
  881. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  882. return 0;
  883. return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
  884. }
  885. static atomic_long_t nfs_attr_generation_counter;
  886. static unsigned long nfs_read_attr_generation_counter(void)
  887. {
  888. return atomic_long_read(&nfs_attr_generation_counter);
  889. }
  890. unsigned long nfs_inc_attr_generation_counter(void)
  891. {
  892. return atomic_long_inc_return(&nfs_attr_generation_counter);
  893. }
  894. void nfs_fattr_init(struct nfs_fattr *fattr)
  895. {
  896. fattr->valid = 0;
  897. fattr->time_start = jiffies;
  898. fattr->gencount = nfs_inc_attr_generation_counter();
  899. fattr->owner_name = NULL;
  900. fattr->group_name = NULL;
  901. }
  902. struct nfs_fattr *nfs_alloc_fattr(void)
  903. {
  904. struct nfs_fattr *fattr;
  905. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  906. if (fattr != NULL)
  907. nfs_fattr_init(fattr);
  908. return fattr;
  909. }
  910. struct nfs_fh *nfs_alloc_fhandle(void)
  911. {
  912. struct nfs_fh *fh;
  913. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  914. if (fh != NULL)
  915. fh->size = 0;
  916. return fh;
  917. }
  918. #ifdef NFS_DEBUG
  919. /*
  920. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  921. * in the same way that wireshark does
  922. *
  923. * @fh: file handle
  924. *
  925. * For debugging only.
  926. */
  927. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  928. {
  929. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  930. * not on the result */
  931. return ~crc32(0xFFFFFFFF, &fh->data[0], fh->size);
  932. }
  933. /*
  934. * _nfs_display_fhandle - display an NFS file handle on the console
  935. *
  936. * @fh: file handle to display
  937. * @caption: display caption
  938. *
  939. * For debugging only.
  940. */
  941. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  942. {
  943. unsigned short i;
  944. if (fh == NULL || fh->size == 0) {
  945. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  946. return;
  947. }
  948. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  949. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  950. for (i = 0; i < fh->size; i += 16) {
  951. __be32 *pos = (__be32 *)&fh->data[i];
  952. switch ((fh->size - i - 1) >> 2) {
  953. case 0:
  954. printk(KERN_DEFAULT " %08x\n",
  955. be32_to_cpup(pos));
  956. break;
  957. case 1:
  958. printk(KERN_DEFAULT " %08x %08x\n",
  959. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  960. break;
  961. case 2:
  962. printk(KERN_DEFAULT " %08x %08x %08x\n",
  963. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  964. be32_to_cpup(pos + 2));
  965. break;
  966. default:
  967. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  968. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  969. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  970. }
  971. }
  972. }
  973. #endif
  974. /**
  975. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  976. * @inode - pointer to inode
  977. * @fattr - attributes
  978. *
  979. * Attempt to divine whether or not an RPC call reply carrying stale
  980. * attributes got scheduled after another call carrying updated ones.
  981. *
  982. * To do so, the function first assumes that a more recent ctime means
  983. * that the attributes in fattr are newer, however it also attempt to
  984. * catch the case where ctime either didn't change, or went backwards
  985. * (if someone reset the clock on the server) by looking at whether
  986. * or not this RPC call was started after the inode was last updated.
  987. * Note also the check for wraparound of 'attr_gencount'
  988. *
  989. * The function returns 'true' if it thinks the attributes in 'fattr' are
  990. * more recent than the ones cached in the inode.
  991. *
  992. */
  993. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  994. {
  995. const struct nfs_inode *nfsi = NFS_I(inode);
  996. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  997. nfs_ctime_need_update(inode, fattr) ||
  998. nfs_size_need_update(inode, fattr) ||
  999. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1000. }
  1001. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1002. {
  1003. if (nfs_inode_attrs_need_update(inode, fattr))
  1004. return nfs_update_inode(inode, fattr);
  1005. return nfs_check_inode_attributes(inode, fattr);
  1006. }
  1007. /**
  1008. * nfs_refresh_inode - try to update the inode attribute cache
  1009. * @inode - pointer to inode
  1010. * @fattr - updated attributes
  1011. *
  1012. * Check that an RPC call that returned attributes has not overlapped with
  1013. * other recent updates of the inode metadata, then decide whether it is
  1014. * safe to do a full update of the inode attributes, or whether just to
  1015. * call nfs_check_inode_attributes.
  1016. */
  1017. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1018. {
  1019. int status;
  1020. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1021. return 0;
  1022. spin_lock(&inode->i_lock);
  1023. status = nfs_refresh_inode_locked(inode, fattr);
  1024. spin_unlock(&inode->i_lock);
  1025. return status;
  1026. }
  1027. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1028. {
  1029. struct nfs_inode *nfsi = NFS_I(inode);
  1030. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1031. if (S_ISDIR(inode->i_mode))
  1032. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  1033. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1034. return 0;
  1035. return nfs_refresh_inode_locked(inode, fattr);
  1036. }
  1037. /**
  1038. * nfs_post_op_update_inode - try to update the inode attribute cache
  1039. * @inode - pointer to inode
  1040. * @fattr - updated attributes
  1041. *
  1042. * After an operation that has changed the inode metadata, mark the
  1043. * attribute cache as being invalid, then try to update it.
  1044. *
  1045. * NB: if the server didn't return any post op attributes, this
  1046. * function will force the retrieval of attributes before the next
  1047. * NFS request. Thus it should be used only for operations that
  1048. * are expected to change one or more attributes, to avoid
  1049. * unnecessary NFS requests and trips through nfs_update_inode().
  1050. */
  1051. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1052. {
  1053. int status;
  1054. spin_lock(&inode->i_lock);
  1055. status = nfs_post_op_update_inode_locked(inode, fattr);
  1056. spin_unlock(&inode->i_lock);
  1057. return status;
  1058. }
  1059. /**
  1060. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1061. * @inode - pointer to inode
  1062. * @fattr - updated attributes
  1063. *
  1064. * After an operation that has changed the inode metadata, mark the
  1065. * attribute cache as being invalid, then try to update it. Fake up
  1066. * weak cache consistency data, if none exist.
  1067. *
  1068. * This function is mainly designed to be used by the ->write_done() functions.
  1069. */
  1070. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1071. {
  1072. int status;
  1073. spin_lock(&inode->i_lock);
  1074. /* Don't do a WCC update if these attributes are already stale */
  1075. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1076. !nfs_inode_attrs_need_update(inode, fattr)) {
  1077. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1078. | NFS_ATTR_FATTR_PRESIZE
  1079. | NFS_ATTR_FATTR_PREMTIME
  1080. | NFS_ATTR_FATTR_PRECTIME);
  1081. goto out_noforce;
  1082. }
  1083. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1084. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1085. fattr->pre_change_attr = inode->i_version;
  1086. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1087. }
  1088. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1089. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1090. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  1091. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1092. }
  1093. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1094. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1095. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  1096. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1097. }
  1098. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1099. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1100. fattr->pre_size = i_size_read(inode);
  1101. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1102. }
  1103. out_noforce:
  1104. status = nfs_post_op_update_inode_locked(inode, fattr);
  1105. spin_unlock(&inode->i_lock);
  1106. return status;
  1107. }
  1108. /*
  1109. * Many nfs protocol calls return the new file attributes after
  1110. * an operation. Here we update the inode to reflect the state
  1111. * of the server's inode.
  1112. *
  1113. * This is a bit tricky because we have to make sure all dirty pages
  1114. * have been sent off to the server before calling invalidate_inode_pages.
  1115. * To make sure no other process adds more write requests while we try
  1116. * our best to flush them, we make them sleep during the attribute refresh.
  1117. *
  1118. * A very similar scenario holds for the dir cache.
  1119. */
  1120. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1121. {
  1122. struct nfs_server *server;
  1123. struct nfs_inode *nfsi = NFS_I(inode);
  1124. loff_t cur_isize, new_isize;
  1125. unsigned long invalid = 0;
  1126. unsigned long now = jiffies;
  1127. unsigned long save_cache_validity;
  1128. dfprintk(VFS, "NFS: %s(%s/%ld fh_crc=0x%08x ct=%d info=0x%x)\n",
  1129. __func__, inode->i_sb->s_id, inode->i_ino,
  1130. nfs_display_fhandle_hash(NFS_FH(inode)),
  1131. atomic_read(&inode->i_count), fattr->valid);
  1132. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1133. goto out_fileid;
  1134. /*
  1135. * Make sure the inode's type hasn't changed.
  1136. */
  1137. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1138. goto out_changed;
  1139. server = NFS_SERVER(inode);
  1140. /* Update the fsid? */
  1141. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1142. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1143. !IS_AUTOMOUNT(inode))
  1144. server->fsid = fattr->fsid;
  1145. /*
  1146. * Update the read time so we don't revalidate too often.
  1147. */
  1148. nfsi->read_cache_jiffies = fattr->time_start;
  1149. save_cache_validity = nfsi->cache_validity;
  1150. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1151. | NFS_INO_INVALID_ATIME
  1152. | NFS_INO_REVAL_FORCED
  1153. | NFS_INO_REVAL_PAGECACHE);
  1154. /* Do atomic weak cache consistency updates */
  1155. invalid |= nfs_wcc_update_inode(inode, fattr);
  1156. /* More cache consistency checks */
  1157. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1158. if (inode->i_version != fattr->change_attr) {
  1159. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1160. inode->i_sb->s_id, inode->i_ino);
  1161. invalid |= NFS_INO_INVALID_ATTR
  1162. | NFS_INO_INVALID_DATA
  1163. | NFS_INO_INVALID_ACCESS
  1164. | NFS_INO_INVALID_ACL
  1165. | NFS_INO_REVAL_PAGECACHE;
  1166. if (S_ISDIR(inode->i_mode))
  1167. nfs_force_lookup_revalidate(inode);
  1168. inode->i_version = fattr->change_attr;
  1169. }
  1170. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1171. invalid |= save_cache_validity;
  1172. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1173. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1174. } else if (server->caps & NFS_CAP_MTIME)
  1175. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1176. | NFS_INO_REVAL_FORCED);
  1177. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1178. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1179. } else if (server->caps & NFS_CAP_CTIME)
  1180. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1181. | NFS_INO_REVAL_FORCED);
  1182. /* Check if our cached file size is stale */
  1183. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1184. new_isize = nfs_size_to_loff_t(fattr->size);
  1185. cur_isize = i_size_read(inode);
  1186. if (new_isize != cur_isize) {
  1187. /* Do we perhaps have any outstanding writes, or has
  1188. * the file grown beyond our last write? */
  1189. if ((nfsi->npages == 0 && !test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) ||
  1190. new_isize > cur_isize) {
  1191. i_size_write(inode, new_isize);
  1192. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1193. }
  1194. dprintk("NFS: isize change on server for file %s/%ld "
  1195. "(%Ld to %Ld)\n",
  1196. inode->i_sb->s_id,
  1197. inode->i_ino,
  1198. (long long)cur_isize,
  1199. (long long)new_isize);
  1200. }
  1201. } else
  1202. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1203. | NFS_INO_REVAL_PAGECACHE
  1204. | NFS_INO_REVAL_FORCED);
  1205. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1206. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1207. else if (server->caps & NFS_CAP_ATIME)
  1208. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1209. | NFS_INO_REVAL_FORCED);
  1210. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1211. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1212. umode_t newmode = inode->i_mode & S_IFMT;
  1213. newmode |= fattr->mode & S_IALLUGO;
  1214. inode->i_mode = newmode;
  1215. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1216. }
  1217. } else if (server->caps & NFS_CAP_MODE)
  1218. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1219. | NFS_INO_INVALID_ACCESS
  1220. | NFS_INO_INVALID_ACL
  1221. | NFS_INO_REVAL_FORCED);
  1222. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1223. if (inode->i_uid != fattr->uid) {
  1224. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1225. inode->i_uid = fattr->uid;
  1226. }
  1227. } else if (server->caps & NFS_CAP_OWNER)
  1228. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1229. | NFS_INO_INVALID_ACCESS
  1230. | NFS_INO_INVALID_ACL
  1231. | NFS_INO_REVAL_FORCED);
  1232. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1233. if (inode->i_gid != fattr->gid) {
  1234. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1235. inode->i_gid = fattr->gid;
  1236. }
  1237. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1238. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1239. | NFS_INO_INVALID_ACCESS
  1240. | NFS_INO_INVALID_ACL
  1241. | NFS_INO_REVAL_FORCED);
  1242. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1243. if (inode->i_nlink != fattr->nlink) {
  1244. invalid |= NFS_INO_INVALID_ATTR;
  1245. if (S_ISDIR(inode->i_mode))
  1246. invalid |= NFS_INO_INVALID_DATA;
  1247. set_nlink(inode, fattr->nlink);
  1248. }
  1249. } else if (server->caps & NFS_CAP_NLINK)
  1250. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1251. | NFS_INO_REVAL_FORCED);
  1252. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1253. /*
  1254. * report the blocks in 512byte units
  1255. */
  1256. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1257. }
  1258. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1259. inode->i_blocks = fattr->du.nfs2.blocks;
  1260. /* Update attrtimeo value if we're out of the unstable period */
  1261. if (invalid & NFS_INO_INVALID_ATTR) {
  1262. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1263. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1264. nfsi->attrtimeo_timestamp = now;
  1265. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1266. } else {
  1267. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1268. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1269. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1270. nfsi->attrtimeo_timestamp = now;
  1271. }
  1272. }
  1273. invalid &= ~NFS_INO_INVALID_ATTR;
  1274. /* Don't invalidate the data if we were to blame */
  1275. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1276. || S_ISLNK(inode->i_mode)))
  1277. invalid &= ~NFS_INO_INVALID_DATA;
  1278. if (!nfs_have_delegation(inode, FMODE_READ) ||
  1279. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1280. nfsi->cache_validity |= invalid;
  1281. return 0;
  1282. out_changed:
  1283. /*
  1284. * Big trouble! The inode has become a different object.
  1285. */
  1286. printk(KERN_DEBUG "NFS: %s: inode %ld mode changed, %07o to %07o\n",
  1287. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1288. out_err:
  1289. /*
  1290. * No need to worry about unhashing the dentry, as the
  1291. * lookup validation will know that the inode is bad.
  1292. * (But we fall through to invalidate the caches.)
  1293. */
  1294. nfs_invalidate_inode(inode);
  1295. return -ESTALE;
  1296. out_fileid:
  1297. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1298. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1299. NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
  1300. (long long)nfsi->fileid, (long long)fattr->fileid);
  1301. goto out_err;
  1302. }
  1303. #ifdef CONFIG_NFS_V4
  1304. /*
  1305. * Clean out any remaining NFSv4 state that might be left over due
  1306. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1307. * nfs_open().
  1308. */
  1309. void nfs4_evict_inode(struct inode *inode)
  1310. {
  1311. truncate_inode_pages(&inode->i_data, 0);
  1312. end_writeback(inode);
  1313. pnfs_return_layout(inode);
  1314. pnfs_destroy_layout(NFS_I(inode));
  1315. /* If we are holding a delegation, return it! */
  1316. nfs_inode_return_delegation_noreclaim(inode);
  1317. /* First call standard NFS clear_inode() code */
  1318. nfs_clear_inode(inode);
  1319. }
  1320. #endif
  1321. struct inode *nfs_alloc_inode(struct super_block *sb)
  1322. {
  1323. struct nfs_inode *nfsi;
  1324. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1325. if (!nfsi)
  1326. return NULL;
  1327. nfsi->flags = 0UL;
  1328. nfsi->cache_validity = 0UL;
  1329. #ifdef CONFIG_NFS_V3_ACL
  1330. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1331. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1332. #endif
  1333. #ifdef CONFIG_NFS_V4
  1334. nfsi->nfs4_acl = NULL;
  1335. #endif /* CONFIG_NFS_V4 */
  1336. return &nfsi->vfs_inode;
  1337. }
  1338. static void nfs_i_callback(struct rcu_head *head)
  1339. {
  1340. struct inode *inode = container_of(head, struct inode, i_rcu);
  1341. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1342. }
  1343. void nfs_destroy_inode(struct inode *inode)
  1344. {
  1345. call_rcu(&inode->i_rcu, nfs_i_callback);
  1346. }
  1347. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1348. {
  1349. #ifdef CONFIG_NFS_V4
  1350. INIT_LIST_HEAD(&nfsi->open_states);
  1351. nfsi->delegation = NULL;
  1352. nfsi->delegation_state = 0;
  1353. init_rwsem(&nfsi->rwsem);
  1354. nfsi->layout = NULL;
  1355. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1356. #endif
  1357. }
  1358. static void init_once(void *foo)
  1359. {
  1360. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1361. inode_init_once(&nfsi->vfs_inode);
  1362. INIT_LIST_HEAD(&nfsi->open_files);
  1363. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1364. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1365. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1366. nfsi->npages = 0;
  1367. nfsi->commit_info.ncommit = 0;
  1368. atomic_set(&nfsi->silly_count, 1);
  1369. INIT_HLIST_HEAD(&nfsi->silly_list);
  1370. init_waitqueue_head(&nfsi->waitqueue);
  1371. nfs4_init_once(nfsi);
  1372. }
  1373. static int __init nfs_init_inodecache(void)
  1374. {
  1375. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1376. sizeof(struct nfs_inode),
  1377. 0, (SLAB_RECLAIM_ACCOUNT|
  1378. SLAB_MEM_SPREAD),
  1379. init_once);
  1380. if (nfs_inode_cachep == NULL)
  1381. return -ENOMEM;
  1382. return 0;
  1383. }
  1384. static void nfs_destroy_inodecache(void)
  1385. {
  1386. kmem_cache_destroy(nfs_inode_cachep);
  1387. }
  1388. struct workqueue_struct *nfsiod_workqueue;
  1389. /*
  1390. * start up the nfsiod workqueue
  1391. */
  1392. static int nfsiod_start(void)
  1393. {
  1394. struct workqueue_struct *wq;
  1395. dprintk("RPC: creating workqueue nfsiod\n");
  1396. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1397. if (wq == NULL)
  1398. return -ENOMEM;
  1399. nfsiod_workqueue = wq;
  1400. return 0;
  1401. }
  1402. /*
  1403. * Destroy the nfsiod workqueue
  1404. */
  1405. static void nfsiod_stop(void)
  1406. {
  1407. struct workqueue_struct *wq;
  1408. wq = nfsiod_workqueue;
  1409. if (wq == NULL)
  1410. return;
  1411. nfsiod_workqueue = NULL;
  1412. destroy_workqueue(wq);
  1413. }
  1414. int nfs_net_id;
  1415. EXPORT_SYMBOL_GPL(nfs_net_id);
  1416. static int nfs_net_init(struct net *net)
  1417. {
  1418. nfs_clients_init(net);
  1419. return nfs_dns_resolver_cache_init(net);
  1420. }
  1421. static void nfs_net_exit(struct net *net)
  1422. {
  1423. nfs_dns_resolver_cache_destroy(net);
  1424. nfs_cleanup_cb_ident_idr(net);
  1425. }
  1426. static struct pernet_operations nfs_net_ops = {
  1427. .init = nfs_net_init,
  1428. .exit = nfs_net_exit,
  1429. .id = &nfs_net_id,
  1430. .size = sizeof(struct nfs_net),
  1431. };
  1432. /*
  1433. * Initialize NFS
  1434. */
  1435. static int __init init_nfs_fs(void)
  1436. {
  1437. int err;
  1438. err = nfs_idmap_init();
  1439. if (err < 0)
  1440. goto out10;
  1441. err = nfs_dns_resolver_init();
  1442. if (err < 0)
  1443. goto out9;
  1444. err = register_pernet_subsys(&nfs_net_ops);
  1445. if (err < 0)
  1446. goto out8;
  1447. err = nfs_fscache_register();
  1448. if (err < 0)
  1449. goto out7;
  1450. err = nfsiod_start();
  1451. if (err)
  1452. goto out6;
  1453. err = nfs_fs_proc_init();
  1454. if (err)
  1455. goto out5;
  1456. err = nfs_init_nfspagecache();
  1457. if (err)
  1458. goto out4;
  1459. err = nfs_init_inodecache();
  1460. if (err)
  1461. goto out3;
  1462. err = nfs_init_readpagecache();
  1463. if (err)
  1464. goto out2;
  1465. err = nfs_init_writepagecache();
  1466. if (err)
  1467. goto out1;
  1468. err = nfs_init_directcache();
  1469. if (err)
  1470. goto out0;
  1471. #ifdef CONFIG_PROC_FS
  1472. rpc_proc_register(&init_net, &nfs_rpcstat);
  1473. #endif
  1474. if ((err = register_nfs_fs()) != 0)
  1475. goto out;
  1476. return 0;
  1477. out:
  1478. #ifdef CONFIG_PROC_FS
  1479. rpc_proc_unregister(&init_net, "nfs");
  1480. #endif
  1481. nfs_destroy_directcache();
  1482. out0:
  1483. nfs_destroy_writepagecache();
  1484. out1:
  1485. nfs_destroy_readpagecache();
  1486. out2:
  1487. nfs_destroy_inodecache();
  1488. out3:
  1489. nfs_destroy_nfspagecache();
  1490. out4:
  1491. nfs_fs_proc_exit();
  1492. out5:
  1493. nfsiod_stop();
  1494. out6:
  1495. nfs_fscache_unregister();
  1496. out7:
  1497. unregister_pernet_subsys(&nfs_net_ops);
  1498. out8:
  1499. nfs_dns_resolver_destroy();
  1500. out9:
  1501. nfs_idmap_quit();
  1502. out10:
  1503. return err;
  1504. }
  1505. static void __exit exit_nfs_fs(void)
  1506. {
  1507. nfs_destroy_directcache();
  1508. nfs_destroy_writepagecache();
  1509. nfs_destroy_readpagecache();
  1510. nfs_destroy_inodecache();
  1511. nfs_destroy_nfspagecache();
  1512. nfs_fscache_unregister();
  1513. unregister_pernet_subsys(&nfs_net_ops);
  1514. nfs_dns_resolver_destroy();
  1515. nfs_idmap_quit();
  1516. #ifdef CONFIG_PROC_FS
  1517. rpc_proc_unregister(&init_net, "nfs");
  1518. #endif
  1519. unregister_nfs_fs();
  1520. nfs_fs_proc_exit();
  1521. nfsiod_stop();
  1522. }
  1523. /* Not quite true; I just maintain it */
  1524. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1525. MODULE_LICENSE("GPL");
  1526. module_param(enable_ino64, bool, 0644);
  1527. module_init(init_nfs_fs)
  1528. module_exit(exit_nfs_fs)