inode.c 47 KB

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