inode.c 48 KB

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