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