inode.c 48 KB

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