inode.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  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. nfs_sb_deactive(sb);
  621. kfree(ctx->mdsthreshold);
  622. kfree(ctx);
  623. }
  624. void put_nfs_open_context(struct nfs_open_context *ctx)
  625. {
  626. __put_nfs_open_context(ctx, 0);
  627. }
  628. EXPORT_SYMBOL_GPL(put_nfs_open_context);
  629. /*
  630. * Ensure that mmap has a recent RPC credential for use when writing out
  631. * shared pages
  632. */
  633. void nfs_file_set_open_context(struct file *filp, struct nfs_open_context *ctx)
  634. {
  635. struct inode *inode = filp->f_path.dentry->d_inode;
  636. struct nfs_inode *nfsi = NFS_I(inode);
  637. filp->private_data = get_nfs_open_context(ctx);
  638. spin_lock(&inode->i_lock);
  639. list_add(&ctx->list, &nfsi->open_files);
  640. spin_unlock(&inode->i_lock);
  641. }
  642. EXPORT_SYMBOL_GPL(nfs_file_set_open_context);
  643. /*
  644. * Given an inode, search for an open context with the desired characteristics
  645. */
  646. struct nfs_open_context *nfs_find_open_context(struct inode *inode, struct rpc_cred *cred, fmode_t mode)
  647. {
  648. struct nfs_inode *nfsi = NFS_I(inode);
  649. struct nfs_open_context *pos, *ctx = NULL;
  650. spin_lock(&inode->i_lock);
  651. list_for_each_entry(pos, &nfsi->open_files, list) {
  652. if (cred != NULL && pos->cred != cred)
  653. continue;
  654. if ((pos->mode & (FMODE_READ|FMODE_WRITE)) != mode)
  655. continue;
  656. ctx = get_nfs_open_context(pos);
  657. break;
  658. }
  659. spin_unlock(&inode->i_lock);
  660. return ctx;
  661. }
  662. static void nfs_file_clear_open_context(struct file *filp)
  663. {
  664. struct inode *inode = filp->f_path.dentry->d_inode;
  665. struct nfs_open_context *ctx = nfs_file_open_context(filp);
  666. if (ctx) {
  667. filp->private_data = NULL;
  668. spin_lock(&inode->i_lock);
  669. list_move_tail(&ctx->list, &NFS_I(inode)->open_files);
  670. spin_unlock(&inode->i_lock);
  671. __put_nfs_open_context(ctx, filp->f_flags & O_DIRECT ? 0 : 1);
  672. }
  673. }
  674. /*
  675. * These allocate and release file read/write context information.
  676. */
  677. int nfs_open(struct inode *inode, struct file *filp)
  678. {
  679. struct nfs_open_context *ctx;
  680. ctx = alloc_nfs_open_context(filp->f_path.dentry, filp->f_mode);
  681. if (IS_ERR(ctx))
  682. return PTR_ERR(ctx);
  683. nfs_file_set_open_context(filp, ctx);
  684. put_nfs_open_context(ctx);
  685. nfs_fscache_set_inode_cookie(inode, filp);
  686. return 0;
  687. }
  688. int nfs_release(struct inode *inode, struct file *filp)
  689. {
  690. nfs_file_clear_open_context(filp);
  691. return 0;
  692. }
  693. /*
  694. * This function is called whenever some part of NFS notices that
  695. * the cached attributes have to be refreshed.
  696. */
  697. int
  698. __nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  699. {
  700. int status = -ESTALE;
  701. struct nfs_fattr *fattr = NULL;
  702. struct nfs_inode *nfsi = NFS_I(inode);
  703. dfprintk(PAGECACHE, "NFS: revalidating (%s/%Ld)\n",
  704. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  705. if (is_bad_inode(inode))
  706. goto out;
  707. if (NFS_STALE(inode))
  708. goto out;
  709. status = -ENOMEM;
  710. fattr = nfs_alloc_fattr();
  711. if (fattr == NULL)
  712. goto out;
  713. nfs_inc_stats(inode, NFSIOS_INODEREVALIDATE);
  714. status = NFS_PROTO(inode)->getattr(server, NFS_FH(inode), fattr);
  715. if (status != 0) {
  716. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) getattr failed, error=%d\n",
  717. inode->i_sb->s_id,
  718. (long long)NFS_FILEID(inode), status);
  719. if (status == -ESTALE) {
  720. nfs_zap_caches(inode);
  721. if (!S_ISDIR(inode->i_mode))
  722. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  723. }
  724. goto out;
  725. }
  726. status = nfs_refresh_inode(inode, fattr);
  727. if (status) {
  728. dfprintk(PAGECACHE, "nfs_revalidate_inode: (%s/%Ld) refresh failed, error=%d\n",
  729. inode->i_sb->s_id,
  730. (long long)NFS_FILEID(inode), status);
  731. goto out;
  732. }
  733. if (nfsi->cache_validity & NFS_INO_INVALID_ACL)
  734. nfs_zap_acl_cache(inode);
  735. dfprintk(PAGECACHE, "NFS: (%s/%Ld) revalidation complete\n",
  736. inode->i_sb->s_id,
  737. (long long)NFS_FILEID(inode));
  738. out:
  739. nfs_free_fattr(fattr);
  740. return status;
  741. }
  742. int nfs_attribute_timeout(struct inode *inode)
  743. {
  744. struct nfs_inode *nfsi = NFS_I(inode);
  745. return !time_in_range_open(jiffies, nfsi->read_cache_jiffies, nfsi->read_cache_jiffies + nfsi->attrtimeo);
  746. }
  747. static int nfs_attribute_cache_expired(struct inode *inode)
  748. {
  749. if (nfs_have_delegated_attributes(inode))
  750. return 0;
  751. return nfs_attribute_timeout(inode);
  752. }
  753. /**
  754. * nfs_revalidate_inode - Revalidate the inode attributes
  755. * @server - pointer to nfs_server struct
  756. * @inode - pointer to inode struct
  757. *
  758. * Updates inode attribute information by retrieving the data from the server.
  759. */
  760. int nfs_revalidate_inode(struct nfs_server *server, struct inode *inode)
  761. {
  762. if (!(NFS_I(inode)->cache_validity & NFS_INO_INVALID_ATTR)
  763. && !nfs_attribute_cache_expired(inode))
  764. return NFS_STALE(inode) ? -ESTALE : 0;
  765. return __nfs_revalidate_inode(server, inode);
  766. }
  767. EXPORT_SYMBOL_GPL(nfs_revalidate_inode);
  768. static int nfs_invalidate_mapping(struct inode *inode, struct address_space *mapping)
  769. {
  770. struct nfs_inode *nfsi = NFS_I(inode);
  771. if (mapping->nrpages != 0) {
  772. int ret = invalidate_inode_pages2(mapping);
  773. if (ret < 0)
  774. return ret;
  775. }
  776. spin_lock(&inode->i_lock);
  777. nfsi->cache_validity &= ~NFS_INO_INVALID_DATA;
  778. if (S_ISDIR(inode->i_mode))
  779. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  780. spin_unlock(&inode->i_lock);
  781. nfs_inc_stats(inode, NFSIOS_DATAINVALIDATE);
  782. nfs_fscache_wait_on_invalidate(inode);
  783. dfprintk(PAGECACHE, "NFS: (%s/%Ld) data cache invalidated\n",
  784. inode->i_sb->s_id, (long long)NFS_FILEID(inode));
  785. return 0;
  786. }
  787. static bool nfs_mapping_need_revalidate_inode(struct inode *inode)
  788. {
  789. if (nfs_have_delegated_attributes(inode))
  790. return false;
  791. return (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE)
  792. || nfs_attribute_timeout(inode)
  793. || NFS_STALE(inode);
  794. }
  795. /**
  796. * nfs_revalidate_mapping - Revalidate the pagecache
  797. * @inode - pointer to host inode
  798. * @mapping - pointer to mapping
  799. */
  800. int nfs_revalidate_mapping(struct inode *inode, struct address_space *mapping)
  801. {
  802. struct nfs_inode *nfsi = NFS_I(inode);
  803. int ret = 0;
  804. /* swapfiles are not supposed to be shared. */
  805. if (IS_SWAPFILE(inode))
  806. goto out;
  807. if (nfs_mapping_need_revalidate_inode(inode)) {
  808. ret = __nfs_revalidate_inode(NFS_SERVER(inode), inode);
  809. if (ret < 0)
  810. goto out;
  811. }
  812. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  813. ret = nfs_invalidate_mapping(inode, mapping);
  814. out:
  815. return ret;
  816. }
  817. static unsigned long nfs_wcc_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  818. {
  819. struct nfs_inode *nfsi = NFS_I(inode);
  820. unsigned long ret = 0;
  821. if ((fattr->valid & NFS_ATTR_FATTR_PRECHANGE)
  822. && (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  823. && inode->i_version == fattr->pre_change_attr) {
  824. inode->i_version = fattr->change_attr;
  825. if (S_ISDIR(inode->i_mode))
  826. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  827. ret |= NFS_INO_INVALID_ATTR;
  828. }
  829. /* If we have atomic WCC data, we may update some attributes */
  830. if ((fattr->valid & NFS_ATTR_FATTR_PRECTIME)
  831. && (fattr->valid & NFS_ATTR_FATTR_CTIME)
  832. && timespec_equal(&inode->i_ctime, &fattr->pre_ctime)) {
  833. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  834. ret |= NFS_INO_INVALID_ATTR;
  835. }
  836. if ((fattr->valid & NFS_ATTR_FATTR_PREMTIME)
  837. && (fattr->valid & NFS_ATTR_FATTR_MTIME)
  838. && timespec_equal(&inode->i_mtime, &fattr->pre_mtime)) {
  839. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  840. if (S_ISDIR(inode->i_mode))
  841. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  842. ret |= NFS_INO_INVALID_ATTR;
  843. }
  844. if ((fattr->valid & NFS_ATTR_FATTR_PRESIZE)
  845. && (fattr->valid & NFS_ATTR_FATTR_SIZE)
  846. && i_size_read(inode) == nfs_size_to_loff_t(fattr->pre_size)
  847. && nfsi->npages == 0) {
  848. i_size_write(inode, nfs_size_to_loff_t(fattr->size));
  849. ret |= NFS_INO_INVALID_ATTR;
  850. }
  851. if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
  852. nfs_fscache_invalidate(inode);
  853. return ret;
  854. }
  855. /**
  856. * nfs_check_inode_attributes - verify consistency of the inode attribute cache
  857. * @inode - pointer to inode
  858. * @fattr - updated attributes
  859. *
  860. * Verifies the attribute cache. If we have just changed the attributes,
  861. * so that fattr carries weak cache consistency data, then it may
  862. * also update the ctime/mtime/change_attribute.
  863. */
  864. static int nfs_check_inode_attributes(struct inode *inode, struct nfs_fattr *fattr)
  865. {
  866. struct nfs_inode *nfsi = NFS_I(inode);
  867. loff_t cur_size, new_isize;
  868. unsigned long invalid = 0;
  869. if (nfs_have_delegated_attributes(inode))
  870. return 0;
  871. /* Has the inode gone and changed behind our back? */
  872. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  873. return -EIO;
  874. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  875. return -EIO;
  876. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  877. inode->i_version != fattr->change_attr)
  878. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  879. /* Verify a few of the more important attributes */
  880. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) && !timespec_equal(&inode->i_mtime, &fattr->mtime))
  881. invalid |= NFS_INO_INVALID_ATTR;
  882. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  883. cur_size = i_size_read(inode);
  884. new_isize = nfs_size_to_loff_t(fattr->size);
  885. if (cur_size != new_isize && nfsi->npages == 0)
  886. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  887. }
  888. /* Have any file permissions changed? */
  889. if ((fattr->valid & NFS_ATTR_FATTR_MODE) && (inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO))
  890. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  891. if ((fattr->valid & NFS_ATTR_FATTR_OWNER) && inode->i_uid != fattr->uid)
  892. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  893. if ((fattr->valid & NFS_ATTR_FATTR_GROUP) && inode->i_gid != fattr->gid)
  894. invalid |= NFS_INO_INVALID_ATTR | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL;
  895. /* Has the link count changed? */
  896. if ((fattr->valid & NFS_ATTR_FATTR_NLINK) && inode->i_nlink != fattr->nlink)
  897. invalid |= NFS_INO_INVALID_ATTR;
  898. if ((fattr->valid & NFS_ATTR_FATTR_ATIME) && !timespec_equal(&inode->i_atime, &fattr->atime))
  899. invalid |= NFS_INO_INVALID_ATIME;
  900. if (invalid != 0)
  901. nfsi->cache_validity |= invalid;
  902. nfsi->read_cache_jiffies = fattr->time_start;
  903. return 0;
  904. }
  905. static int nfs_ctime_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  906. {
  907. if (!(fattr->valid & NFS_ATTR_FATTR_CTIME))
  908. return 0;
  909. return timespec_compare(&fattr->ctime, &inode->i_ctime) > 0;
  910. }
  911. static int nfs_size_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  912. {
  913. if (!(fattr->valid & NFS_ATTR_FATTR_SIZE))
  914. return 0;
  915. return nfs_size_to_loff_t(fattr->size) > i_size_read(inode);
  916. }
  917. static atomic_long_t nfs_attr_generation_counter;
  918. static unsigned long nfs_read_attr_generation_counter(void)
  919. {
  920. return atomic_long_read(&nfs_attr_generation_counter);
  921. }
  922. unsigned long nfs_inc_attr_generation_counter(void)
  923. {
  924. return atomic_long_inc_return(&nfs_attr_generation_counter);
  925. }
  926. void nfs_fattr_init(struct nfs_fattr *fattr)
  927. {
  928. fattr->valid = 0;
  929. fattr->time_start = jiffies;
  930. fattr->gencount = nfs_inc_attr_generation_counter();
  931. fattr->owner_name = NULL;
  932. fattr->group_name = NULL;
  933. }
  934. EXPORT_SYMBOL_GPL(nfs_fattr_init);
  935. struct nfs_fattr *nfs_alloc_fattr(void)
  936. {
  937. struct nfs_fattr *fattr;
  938. fattr = kmalloc(sizeof(*fattr), GFP_NOFS);
  939. if (fattr != NULL)
  940. nfs_fattr_init(fattr);
  941. return fattr;
  942. }
  943. EXPORT_SYMBOL_GPL(nfs_alloc_fattr);
  944. struct nfs_fh *nfs_alloc_fhandle(void)
  945. {
  946. struct nfs_fh *fh;
  947. fh = kmalloc(sizeof(struct nfs_fh), GFP_NOFS);
  948. if (fh != NULL)
  949. fh->size = 0;
  950. return fh;
  951. }
  952. EXPORT_SYMBOL_GPL(nfs_alloc_fhandle);
  953. #ifdef NFS_DEBUG
  954. /*
  955. * _nfs_display_fhandle_hash - calculate the crc32 hash for the filehandle
  956. * in the same way that wireshark does
  957. *
  958. * @fh: file handle
  959. *
  960. * For debugging only.
  961. */
  962. u32 _nfs_display_fhandle_hash(const struct nfs_fh *fh)
  963. {
  964. /* wireshark uses 32-bit AUTODIN crc and does a bitwise
  965. * not on the result */
  966. return ~crc32(0xFFFFFFFF, &fh->data[0], fh->size);
  967. }
  968. /*
  969. * _nfs_display_fhandle - display an NFS file handle on the console
  970. *
  971. * @fh: file handle to display
  972. * @caption: display caption
  973. *
  974. * For debugging only.
  975. */
  976. void _nfs_display_fhandle(const struct nfs_fh *fh, const char *caption)
  977. {
  978. unsigned short i;
  979. if (fh == NULL || fh->size == 0) {
  980. printk(KERN_DEFAULT "%s at %p is empty\n", caption, fh);
  981. return;
  982. }
  983. printk(KERN_DEFAULT "%s at %p is %u bytes, crc: 0x%08x:\n",
  984. caption, fh, fh->size, _nfs_display_fhandle_hash(fh));
  985. for (i = 0; i < fh->size; i += 16) {
  986. __be32 *pos = (__be32 *)&fh->data[i];
  987. switch ((fh->size - i - 1) >> 2) {
  988. case 0:
  989. printk(KERN_DEFAULT " %08x\n",
  990. be32_to_cpup(pos));
  991. break;
  992. case 1:
  993. printk(KERN_DEFAULT " %08x %08x\n",
  994. be32_to_cpup(pos), be32_to_cpup(pos + 1));
  995. break;
  996. case 2:
  997. printk(KERN_DEFAULT " %08x %08x %08x\n",
  998. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  999. be32_to_cpup(pos + 2));
  1000. break;
  1001. default:
  1002. printk(KERN_DEFAULT " %08x %08x %08x %08x\n",
  1003. be32_to_cpup(pos), be32_to_cpup(pos + 1),
  1004. be32_to_cpup(pos + 2), be32_to_cpup(pos + 3));
  1005. }
  1006. }
  1007. }
  1008. #endif
  1009. /**
  1010. * nfs_inode_attrs_need_update - check if the inode attributes need updating
  1011. * @inode - pointer to inode
  1012. * @fattr - attributes
  1013. *
  1014. * Attempt to divine whether or not an RPC call reply carrying stale
  1015. * attributes got scheduled after another call carrying updated ones.
  1016. *
  1017. * To do so, the function first assumes that a more recent ctime means
  1018. * that the attributes in fattr are newer, however it also attempt to
  1019. * catch the case where ctime either didn't change, or went backwards
  1020. * (if someone reset the clock on the server) by looking at whether
  1021. * or not this RPC call was started after the inode was last updated.
  1022. * Note also the check for wraparound of 'attr_gencount'
  1023. *
  1024. * The function returns 'true' if it thinks the attributes in 'fattr' are
  1025. * more recent than the ones cached in the inode.
  1026. *
  1027. */
  1028. static int nfs_inode_attrs_need_update(const struct inode *inode, const struct nfs_fattr *fattr)
  1029. {
  1030. const struct nfs_inode *nfsi = NFS_I(inode);
  1031. return ((long)fattr->gencount - (long)nfsi->attr_gencount) > 0 ||
  1032. nfs_ctime_need_update(inode, fattr) ||
  1033. nfs_size_need_update(inode, fattr) ||
  1034. ((long)nfsi->attr_gencount - (long)nfs_read_attr_generation_counter() > 0);
  1035. }
  1036. static int nfs_refresh_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1037. {
  1038. if (nfs_inode_attrs_need_update(inode, fattr))
  1039. return nfs_update_inode(inode, fattr);
  1040. return nfs_check_inode_attributes(inode, fattr);
  1041. }
  1042. /**
  1043. * nfs_refresh_inode - try to update the inode attribute cache
  1044. * @inode - pointer to inode
  1045. * @fattr - updated attributes
  1046. *
  1047. * Check that an RPC call that returned attributes has not overlapped with
  1048. * other recent updates of the inode metadata, then decide whether it is
  1049. * safe to do a full update of the inode attributes, or whether just to
  1050. * call nfs_check_inode_attributes.
  1051. */
  1052. int nfs_refresh_inode(struct inode *inode, struct nfs_fattr *fattr)
  1053. {
  1054. int status;
  1055. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1056. return 0;
  1057. spin_lock(&inode->i_lock);
  1058. status = nfs_refresh_inode_locked(inode, fattr);
  1059. spin_unlock(&inode->i_lock);
  1060. return status;
  1061. }
  1062. EXPORT_SYMBOL_GPL(nfs_refresh_inode);
  1063. static int nfs_post_op_update_inode_locked(struct inode *inode, struct nfs_fattr *fattr)
  1064. {
  1065. struct nfs_inode *nfsi = NFS_I(inode);
  1066. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE;
  1067. if (S_ISDIR(inode->i_mode)) {
  1068. nfsi->cache_validity |= NFS_INO_INVALID_DATA;
  1069. nfs_fscache_invalidate(inode);
  1070. }
  1071. if ((fattr->valid & NFS_ATTR_FATTR) == 0)
  1072. return 0;
  1073. return nfs_refresh_inode_locked(inode, fattr);
  1074. }
  1075. /**
  1076. * nfs_post_op_update_inode - try to update the inode attribute cache
  1077. * @inode - pointer to inode
  1078. * @fattr - updated attributes
  1079. *
  1080. * After an operation that has changed the inode metadata, mark the
  1081. * attribute cache as being invalid, then try to update it.
  1082. *
  1083. * NB: if the server didn't return any post op attributes, this
  1084. * function will force the retrieval of attributes before the next
  1085. * NFS request. Thus it should be used only for operations that
  1086. * are expected to change one or more attributes, to avoid
  1087. * unnecessary NFS requests and trips through nfs_update_inode().
  1088. */
  1089. int nfs_post_op_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1090. {
  1091. int status;
  1092. spin_lock(&inode->i_lock);
  1093. status = nfs_post_op_update_inode_locked(inode, fattr);
  1094. spin_unlock(&inode->i_lock);
  1095. return status;
  1096. }
  1097. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode);
  1098. /**
  1099. * nfs_post_op_update_inode_force_wcc - try to update the inode attribute cache
  1100. * @inode - pointer to inode
  1101. * @fattr - updated attributes
  1102. *
  1103. * After an operation that has changed the inode metadata, mark the
  1104. * attribute cache as being invalid, then try to update it. Fake up
  1105. * weak cache consistency data, if none exist.
  1106. *
  1107. * This function is mainly designed to be used by the ->write_done() functions.
  1108. */
  1109. int nfs_post_op_update_inode_force_wcc(struct inode *inode, struct nfs_fattr *fattr)
  1110. {
  1111. int status;
  1112. spin_lock(&inode->i_lock);
  1113. /* Don't do a WCC update if these attributes are already stale */
  1114. if ((fattr->valid & NFS_ATTR_FATTR) == 0 ||
  1115. !nfs_inode_attrs_need_update(inode, fattr)) {
  1116. fattr->valid &= ~(NFS_ATTR_FATTR_PRECHANGE
  1117. | NFS_ATTR_FATTR_PRESIZE
  1118. | NFS_ATTR_FATTR_PREMTIME
  1119. | NFS_ATTR_FATTR_PRECTIME);
  1120. goto out_noforce;
  1121. }
  1122. if ((fattr->valid & NFS_ATTR_FATTR_CHANGE) != 0 &&
  1123. (fattr->valid & NFS_ATTR_FATTR_PRECHANGE) == 0) {
  1124. fattr->pre_change_attr = inode->i_version;
  1125. fattr->valid |= NFS_ATTR_FATTR_PRECHANGE;
  1126. }
  1127. if ((fattr->valid & NFS_ATTR_FATTR_CTIME) != 0 &&
  1128. (fattr->valid & NFS_ATTR_FATTR_PRECTIME) == 0) {
  1129. memcpy(&fattr->pre_ctime, &inode->i_ctime, sizeof(fattr->pre_ctime));
  1130. fattr->valid |= NFS_ATTR_FATTR_PRECTIME;
  1131. }
  1132. if ((fattr->valid & NFS_ATTR_FATTR_MTIME) != 0 &&
  1133. (fattr->valid & NFS_ATTR_FATTR_PREMTIME) == 0) {
  1134. memcpy(&fattr->pre_mtime, &inode->i_mtime, sizeof(fattr->pre_mtime));
  1135. fattr->valid |= NFS_ATTR_FATTR_PREMTIME;
  1136. }
  1137. if ((fattr->valid & NFS_ATTR_FATTR_SIZE) != 0 &&
  1138. (fattr->valid & NFS_ATTR_FATTR_PRESIZE) == 0) {
  1139. fattr->pre_size = i_size_read(inode);
  1140. fattr->valid |= NFS_ATTR_FATTR_PRESIZE;
  1141. }
  1142. out_noforce:
  1143. status = nfs_post_op_update_inode_locked(inode, fattr);
  1144. spin_unlock(&inode->i_lock);
  1145. return status;
  1146. }
  1147. EXPORT_SYMBOL_GPL(nfs_post_op_update_inode_force_wcc);
  1148. /*
  1149. * Many nfs protocol calls return the new file attributes after
  1150. * an operation. Here we update the inode to reflect the state
  1151. * of the server's inode.
  1152. *
  1153. * This is a bit tricky because we have to make sure all dirty pages
  1154. * have been sent off to the server before calling invalidate_inode_pages.
  1155. * To make sure no other process adds more write requests while we try
  1156. * our best to flush them, we make them sleep during the attribute refresh.
  1157. *
  1158. * A very similar scenario holds for the dir cache.
  1159. */
  1160. static int nfs_update_inode(struct inode *inode, struct nfs_fattr *fattr)
  1161. {
  1162. struct nfs_server *server;
  1163. struct nfs_inode *nfsi = NFS_I(inode);
  1164. loff_t cur_isize, new_isize;
  1165. unsigned long invalid = 0;
  1166. unsigned long now = jiffies;
  1167. unsigned long save_cache_validity;
  1168. dfprintk(VFS, "NFS: %s(%s/%ld fh_crc=0x%08x ct=%d info=0x%x)\n",
  1169. __func__, inode->i_sb->s_id, inode->i_ino,
  1170. nfs_display_fhandle_hash(NFS_FH(inode)),
  1171. atomic_read(&inode->i_count), fattr->valid);
  1172. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid) {
  1173. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1174. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1175. NFS_SERVER(inode)->nfs_client->cl_hostname,
  1176. inode->i_sb->s_id, (long long)nfsi->fileid,
  1177. (long long)fattr->fileid);
  1178. goto out_err;
  1179. }
  1180. /*
  1181. * Make sure the inode's type hasn't changed.
  1182. */
  1183. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT)) {
  1184. /*
  1185. * Big trouble! The inode has become a different object.
  1186. */
  1187. printk(KERN_DEBUG "NFS: %s: inode %ld mode changed, %07o to %07o\n",
  1188. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1189. goto out_err;
  1190. }
  1191. server = NFS_SERVER(inode);
  1192. /* Update the fsid? */
  1193. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1194. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1195. !IS_AUTOMOUNT(inode))
  1196. server->fsid = fattr->fsid;
  1197. /*
  1198. * Update the read time so we don't revalidate too often.
  1199. */
  1200. nfsi->read_cache_jiffies = fattr->time_start;
  1201. save_cache_validity = nfsi->cache_validity;
  1202. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1203. | NFS_INO_INVALID_ATIME
  1204. | NFS_INO_REVAL_FORCED
  1205. | NFS_INO_REVAL_PAGECACHE);
  1206. /* Do atomic weak cache consistency updates */
  1207. invalid |= nfs_wcc_update_inode(inode, fattr);
  1208. /* More cache consistency checks */
  1209. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1210. if (inode->i_version != fattr->change_attr) {
  1211. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1212. inode->i_sb->s_id, inode->i_ino);
  1213. invalid |= NFS_INO_INVALID_ATTR
  1214. | NFS_INO_INVALID_DATA
  1215. | NFS_INO_INVALID_ACCESS
  1216. | NFS_INO_INVALID_ACL
  1217. | NFS_INO_REVAL_PAGECACHE;
  1218. if (S_ISDIR(inode->i_mode))
  1219. nfs_force_lookup_revalidate(inode);
  1220. inode->i_version = fattr->change_attr;
  1221. }
  1222. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1223. invalid |= save_cache_validity;
  1224. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1225. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1226. } else if (server->caps & NFS_CAP_MTIME)
  1227. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1228. | NFS_INO_REVAL_FORCED);
  1229. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1230. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1231. } else if (server->caps & NFS_CAP_CTIME)
  1232. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1233. | NFS_INO_REVAL_FORCED);
  1234. /* Check if our cached file size is stale */
  1235. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1236. new_isize = nfs_size_to_loff_t(fattr->size);
  1237. cur_isize = i_size_read(inode);
  1238. if (new_isize != cur_isize) {
  1239. /* Do we perhaps have any outstanding writes, or has
  1240. * the file grown beyond our last write? */
  1241. if ((nfsi->npages == 0 && !test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) ||
  1242. new_isize > cur_isize) {
  1243. i_size_write(inode, new_isize);
  1244. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1245. }
  1246. dprintk("NFS: isize change on server for file %s/%ld "
  1247. "(%Ld to %Ld)\n",
  1248. inode->i_sb->s_id,
  1249. inode->i_ino,
  1250. (long long)cur_isize,
  1251. (long long)new_isize);
  1252. }
  1253. } else
  1254. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1255. | NFS_INO_REVAL_PAGECACHE
  1256. | NFS_INO_REVAL_FORCED);
  1257. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1258. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1259. else if (server->caps & NFS_CAP_ATIME)
  1260. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1261. | NFS_INO_REVAL_FORCED);
  1262. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1263. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1264. umode_t newmode = inode->i_mode & S_IFMT;
  1265. newmode |= fattr->mode & S_IALLUGO;
  1266. inode->i_mode = newmode;
  1267. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1268. }
  1269. } else if (server->caps & NFS_CAP_MODE)
  1270. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1271. | NFS_INO_INVALID_ACCESS
  1272. | NFS_INO_INVALID_ACL
  1273. | NFS_INO_REVAL_FORCED);
  1274. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1275. if (inode->i_uid != fattr->uid) {
  1276. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1277. inode->i_uid = fattr->uid;
  1278. }
  1279. } else if (server->caps & NFS_CAP_OWNER)
  1280. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1281. | NFS_INO_INVALID_ACCESS
  1282. | NFS_INO_INVALID_ACL
  1283. | NFS_INO_REVAL_FORCED);
  1284. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1285. if (inode->i_gid != fattr->gid) {
  1286. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1287. inode->i_gid = fattr->gid;
  1288. }
  1289. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1290. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1291. | NFS_INO_INVALID_ACCESS
  1292. | NFS_INO_INVALID_ACL
  1293. | NFS_INO_REVAL_FORCED);
  1294. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1295. if (inode->i_nlink != fattr->nlink) {
  1296. invalid |= NFS_INO_INVALID_ATTR;
  1297. if (S_ISDIR(inode->i_mode))
  1298. invalid |= NFS_INO_INVALID_DATA;
  1299. set_nlink(inode, fattr->nlink);
  1300. }
  1301. } else if (server->caps & NFS_CAP_NLINK)
  1302. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1303. | NFS_INO_REVAL_FORCED);
  1304. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1305. /*
  1306. * report the blocks in 512byte units
  1307. */
  1308. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1309. }
  1310. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1311. inode->i_blocks = fattr->du.nfs2.blocks;
  1312. /* Update attrtimeo value if we're out of the unstable period */
  1313. if (invalid & NFS_INO_INVALID_ATTR) {
  1314. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1315. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1316. nfsi->attrtimeo_timestamp = now;
  1317. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1318. } else {
  1319. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1320. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1321. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1322. nfsi->attrtimeo_timestamp = now;
  1323. }
  1324. }
  1325. invalid &= ~NFS_INO_INVALID_ATTR;
  1326. /* Don't invalidate the data if we were to blame */
  1327. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1328. || S_ISLNK(inode->i_mode)))
  1329. invalid &= ~NFS_INO_INVALID_DATA;
  1330. if (!NFS_PROTO(inode)->have_delegation(inode, FMODE_READ) ||
  1331. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1332. nfsi->cache_validity |= invalid;
  1333. if (invalid & NFS_INO_INVALID_DATA)
  1334. nfs_fscache_invalidate(inode);
  1335. return 0;
  1336. out_err:
  1337. /*
  1338. * No need to worry about unhashing the dentry, as the
  1339. * lookup validation will know that the inode is bad.
  1340. * (But we fall through to invalidate the caches.)
  1341. */
  1342. nfs_invalidate_inode(inode);
  1343. return -ESTALE;
  1344. }
  1345. struct inode *nfs_alloc_inode(struct super_block *sb)
  1346. {
  1347. struct nfs_inode *nfsi;
  1348. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1349. if (!nfsi)
  1350. return NULL;
  1351. nfsi->flags = 0UL;
  1352. nfsi->cache_validity = 0UL;
  1353. #ifdef CONFIG_NFS_V3_ACL
  1354. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1355. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1356. #endif
  1357. #if IS_ENABLED(CONFIG_NFS_V4)
  1358. nfsi->nfs4_acl = NULL;
  1359. #endif /* CONFIG_NFS_V4 */
  1360. return &nfsi->vfs_inode;
  1361. }
  1362. EXPORT_SYMBOL_GPL(nfs_alloc_inode);
  1363. static void nfs_i_callback(struct rcu_head *head)
  1364. {
  1365. struct inode *inode = container_of(head, struct inode, i_rcu);
  1366. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1367. }
  1368. void nfs_destroy_inode(struct inode *inode)
  1369. {
  1370. call_rcu(&inode->i_rcu, nfs_i_callback);
  1371. }
  1372. EXPORT_SYMBOL_GPL(nfs_destroy_inode);
  1373. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1374. {
  1375. #if IS_ENABLED(CONFIG_NFS_V4)
  1376. INIT_LIST_HEAD(&nfsi->open_states);
  1377. nfsi->delegation = NULL;
  1378. nfsi->delegation_state = 0;
  1379. init_rwsem(&nfsi->rwsem);
  1380. nfsi->layout = NULL;
  1381. #endif
  1382. }
  1383. static void init_once(void *foo)
  1384. {
  1385. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1386. inode_init_once(&nfsi->vfs_inode);
  1387. INIT_LIST_HEAD(&nfsi->open_files);
  1388. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1389. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1390. INIT_LIST_HEAD(&nfsi->commit_info.list);
  1391. nfsi->npages = 0;
  1392. nfsi->commit_info.ncommit = 0;
  1393. atomic_set(&nfsi->commit_info.rpcs_out, 0);
  1394. atomic_set(&nfsi->silly_count, 1);
  1395. INIT_HLIST_HEAD(&nfsi->silly_list);
  1396. init_waitqueue_head(&nfsi->waitqueue);
  1397. nfs4_init_once(nfsi);
  1398. }
  1399. static int __init nfs_init_inodecache(void)
  1400. {
  1401. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1402. sizeof(struct nfs_inode),
  1403. 0, (SLAB_RECLAIM_ACCOUNT|
  1404. SLAB_MEM_SPREAD),
  1405. init_once);
  1406. if (nfs_inode_cachep == NULL)
  1407. return -ENOMEM;
  1408. return 0;
  1409. }
  1410. static void nfs_destroy_inodecache(void)
  1411. {
  1412. /*
  1413. * Make sure all delayed rcu free inodes are flushed before we
  1414. * destroy cache.
  1415. */
  1416. rcu_barrier();
  1417. kmem_cache_destroy(nfs_inode_cachep);
  1418. }
  1419. struct workqueue_struct *nfsiod_workqueue;
  1420. EXPORT_SYMBOL_GPL(nfsiod_workqueue);
  1421. /*
  1422. * start up the nfsiod workqueue
  1423. */
  1424. static int nfsiod_start(void)
  1425. {
  1426. struct workqueue_struct *wq;
  1427. dprintk("RPC: creating workqueue nfsiod\n");
  1428. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1429. if (wq == NULL)
  1430. return -ENOMEM;
  1431. nfsiod_workqueue = wq;
  1432. return 0;
  1433. }
  1434. /*
  1435. * Destroy the nfsiod workqueue
  1436. */
  1437. static void nfsiod_stop(void)
  1438. {
  1439. struct workqueue_struct *wq;
  1440. wq = nfsiod_workqueue;
  1441. if (wq == NULL)
  1442. return;
  1443. nfsiod_workqueue = NULL;
  1444. destroy_workqueue(wq);
  1445. }
  1446. int nfs_net_id;
  1447. EXPORT_SYMBOL_GPL(nfs_net_id);
  1448. static int nfs_net_init(struct net *net)
  1449. {
  1450. nfs_clients_init(net);
  1451. return nfs_dns_resolver_cache_init(net);
  1452. }
  1453. static void nfs_net_exit(struct net *net)
  1454. {
  1455. nfs_dns_resolver_cache_destroy(net);
  1456. nfs_cleanup_cb_ident_idr(net);
  1457. }
  1458. static struct pernet_operations nfs_net_ops = {
  1459. .init = nfs_net_init,
  1460. .exit = nfs_net_exit,
  1461. .id = &nfs_net_id,
  1462. .size = sizeof(struct nfs_net),
  1463. };
  1464. /*
  1465. * Initialize NFS
  1466. */
  1467. static int __init init_nfs_fs(void)
  1468. {
  1469. int err;
  1470. err = nfs_dns_resolver_init();
  1471. if (err < 0)
  1472. goto out10;;
  1473. err = register_pernet_subsys(&nfs_net_ops);
  1474. if (err < 0)
  1475. goto out9;
  1476. err = nfs_fscache_register();
  1477. if (err < 0)
  1478. goto out8;
  1479. err = nfsiod_start();
  1480. if (err)
  1481. goto out7;
  1482. err = nfs_fs_proc_init();
  1483. if (err)
  1484. goto out6;
  1485. err = nfs_init_nfspagecache();
  1486. if (err)
  1487. goto out5;
  1488. err = nfs_init_inodecache();
  1489. if (err)
  1490. goto out4;
  1491. err = nfs_init_readpagecache();
  1492. if (err)
  1493. goto out3;
  1494. err = nfs_init_writepagecache();
  1495. if (err)
  1496. goto out2;
  1497. err = nfs_init_directcache();
  1498. if (err)
  1499. goto out1;
  1500. #ifdef CONFIG_PROC_FS
  1501. rpc_proc_register(&init_net, &nfs_rpcstat);
  1502. #endif
  1503. if ((err = register_nfs_fs()) != 0)
  1504. goto out0;
  1505. return 0;
  1506. out0:
  1507. #ifdef CONFIG_PROC_FS
  1508. rpc_proc_unregister(&init_net, "nfs");
  1509. #endif
  1510. nfs_destroy_directcache();
  1511. out1:
  1512. nfs_destroy_writepagecache();
  1513. out2:
  1514. nfs_destroy_readpagecache();
  1515. out3:
  1516. nfs_destroy_inodecache();
  1517. out4:
  1518. nfs_destroy_nfspagecache();
  1519. out5:
  1520. nfs_fs_proc_exit();
  1521. out6:
  1522. nfsiod_stop();
  1523. out7:
  1524. nfs_fscache_unregister();
  1525. out8:
  1526. unregister_pernet_subsys(&nfs_net_ops);
  1527. out9:
  1528. nfs_dns_resolver_destroy();
  1529. out10:
  1530. return err;
  1531. }
  1532. static void __exit exit_nfs_fs(void)
  1533. {
  1534. nfs_destroy_directcache();
  1535. nfs_destroy_writepagecache();
  1536. nfs_destroy_readpagecache();
  1537. nfs_destroy_inodecache();
  1538. nfs_destroy_nfspagecache();
  1539. nfs_fscache_unregister();
  1540. unregister_pernet_subsys(&nfs_net_ops);
  1541. nfs_dns_resolver_destroy();
  1542. #ifdef CONFIG_PROC_FS
  1543. rpc_proc_unregister(&init_net, "nfs");
  1544. #endif
  1545. unregister_nfs_fs();
  1546. nfs_fs_proc_exit();
  1547. nfsiod_stop();
  1548. }
  1549. /* Not quite true; I just maintain it */
  1550. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1551. MODULE_LICENSE("GPL");
  1552. module_param(enable_ino64, bool, 0644);
  1553. module_init(init_nfs_fs)
  1554. module_exit(exit_nfs_fs)