inode.c 49 KB

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