inode.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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/system.h>
  43. #include <asm/uaccess.h>
  44. #include "nfs4_fs.h"
  45. #include "callback.h"
  46. #include "delegation.h"
  47. #include "iostat.h"
  48. #include "internal.h"
  49. #include "fscache.h"
  50. #include "dns_resolve.h"
  51. #include "pnfs.h"
  52. #include "netns.h"
  53. #define NFSDBG_FACILITY NFSDBG_VFS
  54. #define NFS_64_BIT_INODE_NUMBERS_ENABLED 1
  55. /* Default is to see 64-bit inode numbers */
  56. static bool enable_ino64 = NFS_64_BIT_INODE_NUMBERS_ENABLED;
  57. static void nfs_invalidate_inode(struct inode *);
  58. static int nfs_update_inode(struct inode *, struct nfs_fattr *);
  59. static struct kmem_cache * nfs_inode_cachep;
  60. static inline unsigned long
  61. nfs_fattr_to_ino_t(struct nfs_fattr *fattr)
  62. {
  63. return nfs_fileid_to_ino_t(fattr->fileid);
  64. }
  65. /**
  66. * nfs_wait_bit_killable - helper for functions that are sleeping on bit locks
  67. * @word: long word containing the bit lock
  68. */
  69. int nfs_wait_bit_killable(void *word)
  70. {
  71. if (fatal_signal_pending(current))
  72. return -ERESTARTSYS;
  73. freezable_schedule();
  74. return 0;
  75. }
  76. /**
  77. * nfs_compat_user_ino64 - returns the user-visible inode number
  78. * @fileid: 64-bit fileid
  79. *
  80. * This function returns a 32-bit inode number if the boot parameter
  81. * nfs.enable_ino64 is zero.
  82. */
  83. u64 nfs_compat_user_ino64(u64 fileid)
  84. {
  85. #ifdef CONFIG_COMPAT
  86. compat_ulong_t ino;
  87. #else
  88. unsigned long ino;
  89. #endif
  90. if (enable_ino64)
  91. return fileid;
  92. ino = fileid;
  93. if (sizeof(ino) < sizeof(fileid))
  94. ino ^= fileid >> (sizeof(fileid)-sizeof(ino)) * 8;
  95. return ino;
  96. }
  97. static void nfs_clear_inode(struct inode *inode)
  98. {
  99. /*
  100. * The following should never happen...
  101. */
  102. BUG_ON(nfs_have_writebacks(inode));
  103. BUG_ON(!list_empty(&NFS_I(inode)->open_files));
  104. nfs_zap_acl_cache(inode);
  105. nfs_access_zap_cache(inode);
  106. nfs_fscache_release_inode_cookie(inode);
  107. }
  108. void nfs_evict_inode(struct inode *inode)
  109. {
  110. truncate_inode_pages(&inode->i_data, 0);
  111. end_writeback(inode);
  112. nfs_clear_inode(inode);
  113. }
  114. /**
  115. * nfs_sync_mapping - helper to flush all mmapped dirty data to disk
  116. */
  117. int nfs_sync_mapping(struct address_space *mapping)
  118. {
  119. int ret = 0;
  120. if (mapping->nrpages != 0) {
  121. unmap_mapping_range(mapping, 0, 0, 0);
  122. ret = nfs_wb_all(mapping->host);
  123. }
  124. return ret;
  125. }
  126. /*
  127. * Invalidate the local caches
  128. */
  129. static void nfs_zap_caches_locked(struct inode *inode)
  130. {
  131. struct nfs_inode *nfsi = NFS_I(inode);
  132. int mode = inode->i_mode;
  133. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  134. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  135. nfsi->attrtimeo_timestamp = jiffies;
  136. memset(NFS_COOKIEVERF(inode), 0, sizeof(NFS_COOKIEVERF(inode)));
  137. if (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))
  138. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  139. else
  140. nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL|NFS_INO_REVAL_PAGECACHE;
  141. }
  142. void nfs_zap_caches(struct inode *inode)
  143. {
  144. spin_lock(&inode->i_lock);
  145. nfs_zap_caches_locked(inode);
  146. spin_unlock(&inode->i_lock);
  147. }
  148. void nfs_zap_mapping(struct inode *inode, struct address_space *mapping)
  149. {
  150. if (mapping->nrpages != 0) {
  151. spin_lock(&inode->i_lock);
  152. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  153. spin_unlock(&inode->i_lock);
  154. }
  155. }
  156. void nfs_zap_acl_cache(struct inode *inode)
  157. {
  158. void (*clear_acl_cache)(struct inode *);
  159. clear_acl_cache = NFS_PROTO(inode)->clear_acl_cache;
  160. if (clear_acl_cache != NULL)
  161. clear_acl_cache(inode);
  162. spin_lock(&inode->i_lock);
  163. NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_ACL;
  164. spin_unlock(&inode->i_lock);
  165. }
  166. void nfs_invalidate_atime(struct inode *inode)
  167. {
  168. spin_lock(&inode->i_lock);
  169. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  170. spin_unlock(&inode->i_lock);
  171. }
  172. /*
  173. * Invalidate, but do not unhash, the inode.
  174. * NB: must be called with inode->i_lock held!
  175. */
  176. static void nfs_invalidate_inode(struct inode *inode)
  177. {
  178. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  179. nfs_zap_caches_locked(inode);
  180. }
  181. struct nfs_find_desc {
  182. struct nfs_fh *fh;
  183. struct nfs_fattr *fattr;
  184. };
  185. /*
  186. * In NFSv3 we can have 64bit inode numbers. In order to support
  187. * this, and re-exported directories (also seen in NFSv2)
  188. * we are forced to allow 2 different inodes to have the same
  189. * i_ino.
  190. */
  191. static int
  192. nfs_find_actor(struct inode *inode, void *opaque)
  193. {
  194. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  195. struct nfs_fh *fh = desc->fh;
  196. struct nfs_fattr *fattr = desc->fattr;
  197. if (NFS_FILEID(inode) != fattr->fileid)
  198. return 0;
  199. if (nfs_compare_fh(NFS_FH(inode), fh))
  200. return 0;
  201. if (is_bad_inode(inode) || NFS_STALE(inode))
  202. return 0;
  203. return 1;
  204. }
  205. static int
  206. nfs_init_locked(struct inode *inode, void *opaque)
  207. {
  208. struct nfs_find_desc *desc = (struct nfs_find_desc *)opaque;
  209. struct nfs_fattr *fattr = desc->fattr;
  210. set_nfs_fileid(inode, fattr->fileid);
  211. nfs_copy_fh(NFS_FH(inode), desc->fh);
  212. return 0;
  213. }
  214. /*
  215. * This is our front-end to iget that looks up inodes by file handle
  216. * instead of inode number.
  217. */
  218. struct inode *
  219. nfs_fhget(struct super_block *sb, struct nfs_fh *fh, struct nfs_fattr *fattr)
  220. {
  221. struct nfs_find_desc desc = {
  222. .fh = fh,
  223. .fattr = fattr
  224. };
  225. struct inode *inode = ERR_PTR(-ENOENT);
  226. unsigned long hash;
  227. nfs_attr_check_mountpoint(sb, fattr);
  228. if (((fattr->valid & NFS_ATTR_FATTR_FILEID) == 0) &&
  229. !nfs_attr_use_mounted_on_fileid(fattr))
  230. goto out_no_inode;
  231. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) == 0)
  232. goto out_no_inode;
  233. hash = nfs_fattr_to_ino_t(fattr);
  234. inode = iget5_locked(sb, hash, nfs_find_actor, nfs_init_locked, &desc);
  235. if (inode == NULL) {
  236. inode = ERR_PTR(-ENOMEM);
  237. goto out_no_inode;
  238. }
  239. if (inode->i_state & I_NEW) {
  240. struct nfs_inode *nfsi = NFS_I(inode);
  241. unsigned long now = jiffies;
  242. /* We set i_ino for the few things that still rely on it,
  243. * such as stat(2) */
  244. inode->i_ino = hash;
  245. /* We can't support update_atime(), since the server will reset it */
  246. inode->i_flags |= S_NOATIME|S_NOCMTIME;
  247. inode->i_mode = fattr->mode;
  248. if ((fattr->valid & NFS_ATTR_FATTR_MODE) == 0
  249. && nfs_server_capable(inode, NFS_CAP_MODE))
  250. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  251. | NFS_INO_INVALID_ACCESS
  252. | NFS_INO_INVALID_ACL;
  253. /* Why so? Because we want revalidate for devices/FIFOs, and
  254. * that's precisely what we have in nfs_file_inode_operations.
  255. */
  256. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->file_inode_ops;
  257. if (S_ISREG(inode->i_mode)) {
  258. inode->i_fop = NFS_SB(sb)->nfs_client->rpc_ops->file_ops;
  259. inode->i_data.a_ops = &nfs_file_aops;
  260. inode->i_data.backing_dev_info = &NFS_SB(sb)->backing_dev_info;
  261. } else if (S_ISDIR(inode->i_mode)) {
  262. inode->i_op = NFS_SB(sb)->nfs_client->rpc_ops->dir_inode_ops;
  263. inode->i_fop = &nfs_dir_operations;
  264. inode->i_data.a_ops = &nfs_dir_aops;
  265. if (nfs_server_capable(inode, NFS_CAP_READDIRPLUS))
  266. set_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  267. /* Deal with crossing mountpoints */
  268. if (fattr->valid & NFS_ATTR_FATTR_MOUNTPOINT ||
  269. fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL) {
  270. if (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)
  271. inode->i_op = &nfs_referral_inode_operations;
  272. else
  273. inode->i_op = &nfs_mountpoint_inode_operations;
  274. inode->i_fop = NULL;
  275. inode->i_flags |= S_AUTOMOUNT;
  276. }
  277. } else if (S_ISLNK(inode->i_mode))
  278. inode->i_op = &nfs_symlink_inode_operations;
  279. else
  280. init_special_inode(inode, inode->i_mode, fattr->rdev);
  281. memset(&inode->i_atime, 0, sizeof(inode->i_atime));
  282. memset(&inode->i_mtime, 0, sizeof(inode->i_mtime));
  283. memset(&inode->i_ctime, 0, sizeof(inode->i_ctime));
  284. inode->i_version = 0;
  285. inode->i_size = 0;
  286. clear_nlink(inode);
  287. inode->i_uid = -2;
  288. inode->i_gid = -2;
  289. inode->i_blocks = 0;
  290. memset(nfsi->cookieverf, 0, sizeof(nfsi->cookieverf));
  291. nfsi->read_cache_jiffies = fattr->time_start;
  292. nfsi->attr_gencount = fattr->gencount;
  293. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  294. inode->i_atime = fattr->atime;
  295. else if (nfs_server_capable(inode, NFS_CAP_ATIME))
  296. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  297. if (fattr->valid & NFS_ATTR_FATTR_MTIME)
  298. inode->i_mtime = fattr->mtime;
  299. else if (nfs_server_capable(inode, NFS_CAP_MTIME))
  300. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  301. | NFS_INO_INVALID_DATA;
  302. if (fattr->valid & NFS_ATTR_FATTR_CTIME)
  303. inode->i_ctime = fattr->ctime;
  304. else if (nfs_server_capable(inode, NFS_CAP_CTIME))
  305. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  306. | NFS_INO_INVALID_ACCESS
  307. | NFS_INO_INVALID_ACL;
  308. if (fattr->valid & NFS_ATTR_FATTR_CHANGE)
  309. inode->i_version = fattr->change_attr;
  310. else if (nfs_server_capable(inode, NFS_CAP_CHANGE_ATTR))
  311. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  312. | NFS_INO_INVALID_DATA;
  313. if (fattr->valid & NFS_ATTR_FATTR_SIZE)
  314. inode->i_size = nfs_size_to_loff_t(fattr->size);
  315. else
  316. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  317. | NFS_INO_INVALID_DATA
  318. | NFS_INO_REVAL_PAGECACHE;
  319. if (fattr->valid & NFS_ATTR_FATTR_NLINK)
  320. set_nlink(inode, fattr->nlink);
  321. else if (nfs_server_capable(inode, NFS_CAP_NLINK))
  322. nfsi->cache_validity |= NFS_INO_INVALID_ATTR;
  323. if (fattr->valid & NFS_ATTR_FATTR_OWNER)
  324. inode->i_uid = fattr->uid;
  325. else if (nfs_server_capable(inode, NFS_CAP_OWNER))
  326. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  327. | NFS_INO_INVALID_ACCESS
  328. | NFS_INO_INVALID_ACL;
  329. if (fattr->valid & NFS_ATTR_FATTR_GROUP)
  330. inode->i_gid = fattr->gid;
  331. else if (nfs_server_capable(inode, NFS_CAP_OWNER_GROUP))
  332. nfsi->cache_validity |= NFS_INO_INVALID_ATTR
  333. | NFS_INO_INVALID_ACCESS
  334. | NFS_INO_INVALID_ACL;
  335. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  336. inode->i_blocks = fattr->du.nfs2.blocks;
  337. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  338. /*
  339. * report the blocks in 512byte units
  340. */
  341. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  342. }
  343. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  344. nfsi->attrtimeo_timestamp = now;
  345. nfsi->access_cache = RB_ROOT;
  346. nfs_fscache_init_inode_cookie(inode);
  347. unlock_new_inode(inode);
  348. } else
  349. nfs_refresh_inode(inode, fattr);
  350. dprintk("NFS: nfs_fhget(%s/%Ld ct=%d)\n",
  351. inode->i_sb->s_id,
  352. (long long)NFS_FILEID(inode),
  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 RPC_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 ct=%d info=0x%x)\n",
  1132. __func__, inode->i_sb->s_id, inode->i_ino,
  1133. atomic_read(&inode->i_count), fattr->valid);
  1134. if ((fattr->valid & NFS_ATTR_FATTR_FILEID) && nfsi->fileid != fattr->fileid)
  1135. goto out_fileid;
  1136. /*
  1137. * Make sure the inode's type hasn't changed.
  1138. */
  1139. if ((fattr->valid & NFS_ATTR_FATTR_TYPE) && (inode->i_mode & S_IFMT) != (fattr->mode & S_IFMT))
  1140. goto out_changed;
  1141. server = NFS_SERVER(inode);
  1142. /* Update the fsid? */
  1143. if (S_ISDIR(inode->i_mode) && (fattr->valid & NFS_ATTR_FATTR_FSID) &&
  1144. !nfs_fsid_equal(&server->fsid, &fattr->fsid) &&
  1145. !IS_AUTOMOUNT(inode))
  1146. server->fsid = fattr->fsid;
  1147. /*
  1148. * Update the read time so we don't revalidate too often.
  1149. */
  1150. nfsi->read_cache_jiffies = fattr->time_start;
  1151. save_cache_validity = nfsi->cache_validity;
  1152. nfsi->cache_validity &= ~(NFS_INO_INVALID_ATTR
  1153. | NFS_INO_INVALID_ATIME
  1154. | NFS_INO_REVAL_FORCED
  1155. | NFS_INO_REVAL_PAGECACHE);
  1156. /* Do atomic weak cache consistency updates */
  1157. invalid |= nfs_wcc_update_inode(inode, fattr);
  1158. /* More cache consistency checks */
  1159. if (fattr->valid & NFS_ATTR_FATTR_CHANGE) {
  1160. if (inode->i_version != fattr->change_attr) {
  1161. dprintk("NFS: change_attr change on server for file %s/%ld\n",
  1162. inode->i_sb->s_id, inode->i_ino);
  1163. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1164. if (S_ISDIR(inode->i_mode))
  1165. nfs_force_lookup_revalidate(inode);
  1166. inode->i_version = fattr->change_attr;
  1167. }
  1168. } else if (server->caps & NFS_CAP_CHANGE_ATTR)
  1169. invalid |= save_cache_validity;
  1170. if (fattr->valid & NFS_ATTR_FATTR_MTIME) {
  1171. /* NFSv2/v3: Check if the mtime agrees */
  1172. if (!timespec_equal(&inode->i_mtime, &fattr->mtime)) {
  1173. dprintk("NFS: mtime change on server for file %s/%ld\n",
  1174. inode->i_sb->s_id, inode->i_ino);
  1175. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1176. if (S_ISDIR(inode->i_mode))
  1177. nfs_force_lookup_revalidate(inode);
  1178. memcpy(&inode->i_mtime, &fattr->mtime, sizeof(inode->i_mtime));
  1179. }
  1180. } else if (server->caps & NFS_CAP_MTIME)
  1181. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1182. | NFS_INO_INVALID_DATA
  1183. | NFS_INO_REVAL_PAGECACHE
  1184. | NFS_INO_REVAL_FORCED);
  1185. if (fattr->valid & NFS_ATTR_FATTR_CTIME) {
  1186. /* If ctime has changed we should definitely clear access+acl caches */
  1187. if (!timespec_equal(&inode->i_ctime, &fattr->ctime)) {
  1188. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1189. /* and probably clear data for a directory too as utimes can cause
  1190. * havoc with our cache.
  1191. */
  1192. if (S_ISDIR(inode->i_mode)) {
  1193. invalid |= NFS_INO_INVALID_DATA;
  1194. nfs_force_lookup_revalidate(inode);
  1195. }
  1196. memcpy(&inode->i_ctime, &fattr->ctime, sizeof(inode->i_ctime));
  1197. }
  1198. } else if (server->caps & NFS_CAP_CTIME)
  1199. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1200. | NFS_INO_INVALID_ACCESS
  1201. | NFS_INO_INVALID_ACL
  1202. | NFS_INO_REVAL_FORCED);
  1203. /* Check if our cached file size is stale */
  1204. if (fattr->valid & NFS_ATTR_FATTR_SIZE) {
  1205. new_isize = nfs_size_to_loff_t(fattr->size);
  1206. cur_isize = i_size_read(inode);
  1207. if (new_isize != cur_isize) {
  1208. /* Do we perhaps have any outstanding writes, or has
  1209. * the file grown beyond our last write? */
  1210. if ((nfsi->npages == 0 && !test_bit(NFS_INO_LAYOUTCOMMIT, &nfsi->flags)) ||
  1211. new_isize > cur_isize) {
  1212. i_size_write(inode, new_isize);
  1213. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
  1214. }
  1215. dprintk("NFS: isize change on server for file %s/%ld "
  1216. "(%Ld to %Ld)\n",
  1217. inode->i_sb->s_id,
  1218. inode->i_ino,
  1219. (long long)cur_isize,
  1220. (long long)new_isize);
  1221. }
  1222. } else
  1223. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1224. | NFS_INO_REVAL_PAGECACHE
  1225. | NFS_INO_REVAL_FORCED);
  1226. if (fattr->valid & NFS_ATTR_FATTR_ATIME)
  1227. memcpy(&inode->i_atime, &fattr->atime, sizeof(inode->i_atime));
  1228. else if (server->caps & NFS_CAP_ATIME)
  1229. invalid |= save_cache_validity & (NFS_INO_INVALID_ATIME
  1230. | NFS_INO_REVAL_FORCED);
  1231. if (fattr->valid & NFS_ATTR_FATTR_MODE) {
  1232. if ((inode->i_mode & S_IALLUGO) != (fattr->mode & S_IALLUGO)) {
  1233. umode_t newmode = inode->i_mode & S_IFMT;
  1234. newmode |= fattr->mode & S_IALLUGO;
  1235. inode->i_mode = newmode;
  1236. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1237. }
  1238. } else if (server->caps & NFS_CAP_MODE)
  1239. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1240. | NFS_INO_INVALID_ACCESS
  1241. | NFS_INO_INVALID_ACL
  1242. | NFS_INO_REVAL_FORCED);
  1243. if (fattr->valid & NFS_ATTR_FATTR_OWNER) {
  1244. if (inode->i_uid != fattr->uid) {
  1245. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1246. inode->i_uid = fattr->uid;
  1247. }
  1248. } else if (server->caps & NFS_CAP_OWNER)
  1249. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1250. | NFS_INO_INVALID_ACCESS
  1251. | NFS_INO_INVALID_ACL
  1252. | NFS_INO_REVAL_FORCED);
  1253. if (fattr->valid & NFS_ATTR_FATTR_GROUP) {
  1254. if (inode->i_gid != fattr->gid) {
  1255. invalid |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ACCESS|NFS_INO_INVALID_ACL;
  1256. inode->i_gid = fattr->gid;
  1257. }
  1258. } else if (server->caps & NFS_CAP_OWNER_GROUP)
  1259. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1260. | NFS_INO_INVALID_ACCESS
  1261. | NFS_INO_INVALID_ACL
  1262. | NFS_INO_REVAL_FORCED);
  1263. if (fattr->valid & NFS_ATTR_FATTR_NLINK) {
  1264. if (inode->i_nlink != fattr->nlink) {
  1265. invalid |= NFS_INO_INVALID_ATTR;
  1266. if (S_ISDIR(inode->i_mode))
  1267. invalid |= NFS_INO_INVALID_DATA;
  1268. set_nlink(inode, fattr->nlink);
  1269. }
  1270. } else if (server->caps & NFS_CAP_NLINK)
  1271. invalid |= save_cache_validity & (NFS_INO_INVALID_ATTR
  1272. | NFS_INO_REVAL_FORCED);
  1273. if (fattr->valid & NFS_ATTR_FATTR_SPACE_USED) {
  1274. /*
  1275. * report the blocks in 512byte units
  1276. */
  1277. inode->i_blocks = nfs_calc_block_size(fattr->du.nfs3.used);
  1278. }
  1279. if (fattr->valid & NFS_ATTR_FATTR_BLOCKS_USED)
  1280. inode->i_blocks = fattr->du.nfs2.blocks;
  1281. /* Update attrtimeo value if we're out of the unstable period */
  1282. if (invalid & NFS_INO_INVALID_ATTR) {
  1283. nfs_inc_stats(inode, NFSIOS_ATTRINVALIDATE);
  1284. nfsi->attrtimeo = NFS_MINATTRTIMEO(inode);
  1285. nfsi->attrtimeo_timestamp = now;
  1286. nfsi->attr_gencount = nfs_inc_attr_generation_counter();
  1287. } else {
  1288. if (!time_in_range_open(now, nfsi->attrtimeo_timestamp, nfsi->attrtimeo_timestamp + nfsi->attrtimeo)) {
  1289. if ((nfsi->attrtimeo <<= 1) > NFS_MAXATTRTIMEO(inode))
  1290. nfsi->attrtimeo = NFS_MAXATTRTIMEO(inode);
  1291. nfsi->attrtimeo_timestamp = now;
  1292. }
  1293. }
  1294. invalid &= ~NFS_INO_INVALID_ATTR;
  1295. /* Don't invalidate the data if we were to blame */
  1296. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
  1297. || S_ISLNK(inode->i_mode)))
  1298. invalid &= ~NFS_INO_INVALID_DATA;
  1299. if (!nfs_have_delegation(inode, FMODE_READ) ||
  1300. (save_cache_validity & NFS_INO_REVAL_FORCED))
  1301. nfsi->cache_validity |= invalid;
  1302. return 0;
  1303. out_changed:
  1304. /*
  1305. * Big trouble! The inode has become a different object.
  1306. */
  1307. printk(KERN_DEBUG "NFS: %s: inode %ld mode changed, %07o to %07o\n",
  1308. __func__, inode->i_ino, inode->i_mode, fattr->mode);
  1309. out_err:
  1310. /*
  1311. * No need to worry about unhashing the dentry, as the
  1312. * lookup validation will know that the inode is bad.
  1313. * (But we fall through to invalidate the caches.)
  1314. */
  1315. nfs_invalidate_inode(inode);
  1316. return -ESTALE;
  1317. out_fileid:
  1318. printk(KERN_ERR "NFS: server %s error: fileid changed\n"
  1319. "fsid %s: expected fileid 0x%Lx, got 0x%Lx\n",
  1320. NFS_SERVER(inode)->nfs_client->cl_hostname, inode->i_sb->s_id,
  1321. (long long)nfsi->fileid, (long long)fattr->fileid);
  1322. goto out_err;
  1323. }
  1324. #ifdef CONFIG_NFS_V4
  1325. /*
  1326. * Clean out any remaining NFSv4 state that might be left over due
  1327. * to open() calls that passed nfs_atomic_lookup, but failed to call
  1328. * nfs_open().
  1329. */
  1330. void nfs4_evict_inode(struct inode *inode)
  1331. {
  1332. truncate_inode_pages(&inode->i_data, 0);
  1333. end_writeback(inode);
  1334. pnfs_return_layout(inode);
  1335. pnfs_destroy_layout(NFS_I(inode));
  1336. /* If we are holding a delegation, return it! */
  1337. nfs_inode_return_delegation_noreclaim(inode);
  1338. /* First call standard NFS clear_inode() code */
  1339. nfs_clear_inode(inode);
  1340. }
  1341. #endif
  1342. struct inode *nfs_alloc_inode(struct super_block *sb)
  1343. {
  1344. struct nfs_inode *nfsi;
  1345. nfsi = (struct nfs_inode *)kmem_cache_alloc(nfs_inode_cachep, GFP_KERNEL);
  1346. if (!nfsi)
  1347. return NULL;
  1348. nfsi->flags = 0UL;
  1349. nfsi->cache_validity = 0UL;
  1350. #ifdef CONFIG_NFS_V3_ACL
  1351. nfsi->acl_access = ERR_PTR(-EAGAIN);
  1352. nfsi->acl_default = ERR_PTR(-EAGAIN);
  1353. #endif
  1354. #ifdef CONFIG_NFS_V4
  1355. nfsi->nfs4_acl = NULL;
  1356. #endif /* CONFIG_NFS_V4 */
  1357. return &nfsi->vfs_inode;
  1358. }
  1359. static void nfs_i_callback(struct rcu_head *head)
  1360. {
  1361. struct inode *inode = container_of(head, struct inode, i_rcu);
  1362. kmem_cache_free(nfs_inode_cachep, NFS_I(inode));
  1363. }
  1364. void nfs_destroy_inode(struct inode *inode)
  1365. {
  1366. call_rcu(&inode->i_rcu, nfs_i_callback);
  1367. }
  1368. static inline void nfs4_init_once(struct nfs_inode *nfsi)
  1369. {
  1370. #ifdef CONFIG_NFS_V4
  1371. INIT_LIST_HEAD(&nfsi->open_states);
  1372. nfsi->delegation = NULL;
  1373. nfsi->delegation_state = 0;
  1374. init_rwsem(&nfsi->rwsem);
  1375. nfsi->layout = NULL;
  1376. atomic_set(&nfsi->commits_outstanding, 0);
  1377. #endif
  1378. }
  1379. static void init_once(void *foo)
  1380. {
  1381. struct nfs_inode *nfsi = (struct nfs_inode *) foo;
  1382. inode_init_once(&nfsi->vfs_inode);
  1383. INIT_LIST_HEAD(&nfsi->open_files);
  1384. INIT_LIST_HEAD(&nfsi->access_cache_entry_lru);
  1385. INIT_LIST_HEAD(&nfsi->access_cache_inode_lru);
  1386. INIT_RADIX_TREE(&nfsi->nfs_page_tree, GFP_ATOMIC);
  1387. nfsi->npages = 0;
  1388. nfsi->ncommit = 0;
  1389. atomic_set(&nfsi->silly_count, 1);
  1390. INIT_HLIST_HEAD(&nfsi->silly_list);
  1391. init_waitqueue_head(&nfsi->waitqueue);
  1392. nfs4_init_once(nfsi);
  1393. }
  1394. static int __init nfs_init_inodecache(void)
  1395. {
  1396. nfs_inode_cachep = kmem_cache_create("nfs_inode_cache",
  1397. sizeof(struct nfs_inode),
  1398. 0, (SLAB_RECLAIM_ACCOUNT|
  1399. SLAB_MEM_SPREAD),
  1400. init_once);
  1401. if (nfs_inode_cachep == NULL)
  1402. return -ENOMEM;
  1403. return 0;
  1404. }
  1405. static void nfs_destroy_inodecache(void)
  1406. {
  1407. kmem_cache_destroy(nfs_inode_cachep);
  1408. }
  1409. struct workqueue_struct *nfsiod_workqueue;
  1410. /*
  1411. * start up the nfsiod workqueue
  1412. */
  1413. static int nfsiod_start(void)
  1414. {
  1415. struct workqueue_struct *wq;
  1416. dprintk("RPC: creating workqueue nfsiod\n");
  1417. wq = alloc_workqueue("nfsiod", WQ_MEM_RECLAIM, 0);
  1418. if (wq == NULL)
  1419. return -ENOMEM;
  1420. nfsiod_workqueue = wq;
  1421. return 0;
  1422. }
  1423. /*
  1424. * Destroy the nfsiod workqueue
  1425. */
  1426. static void nfsiod_stop(void)
  1427. {
  1428. struct workqueue_struct *wq;
  1429. wq = nfsiod_workqueue;
  1430. if (wq == NULL)
  1431. return;
  1432. nfsiod_workqueue = NULL;
  1433. destroy_workqueue(wq);
  1434. }
  1435. int nfs_net_id;
  1436. EXPORT_SYMBOL_GPL(nfs_net_id);
  1437. static int nfs_net_init(struct net *net)
  1438. {
  1439. nfs_clients_init(net);
  1440. return nfs_dns_resolver_cache_init(net);
  1441. }
  1442. static void nfs_net_exit(struct net *net)
  1443. {
  1444. nfs_dns_resolver_cache_destroy(net);
  1445. nfs_cleanup_cb_ident_idr(net);
  1446. }
  1447. static struct pernet_operations nfs_net_ops = {
  1448. .init = nfs_net_init,
  1449. .exit = nfs_net_exit,
  1450. .id = &nfs_net_id,
  1451. .size = sizeof(struct nfs_net),
  1452. };
  1453. /*
  1454. * Initialize NFS
  1455. */
  1456. static int __init init_nfs_fs(void)
  1457. {
  1458. int err;
  1459. err = nfs_idmap_init();
  1460. if (err < 0)
  1461. goto out10;
  1462. err = nfs_dns_resolver_init();
  1463. if (err < 0)
  1464. goto out9;
  1465. err = register_pernet_subsys(&nfs_net_ops);
  1466. if (err < 0)
  1467. goto out8;
  1468. err = nfs_fscache_register();
  1469. if (err < 0)
  1470. goto out7;
  1471. err = nfsiod_start();
  1472. if (err)
  1473. goto out6;
  1474. err = nfs_fs_proc_init();
  1475. if (err)
  1476. goto out5;
  1477. err = nfs_init_nfspagecache();
  1478. if (err)
  1479. goto out4;
  1480. err = nfs_init_inodecache();
  1481. if (err)
  1482. goto out3;
  1483. err = nfs_init_readpagecache();
  1484. if (err)
  1485. goto out2;
  1486. err = nfs_init_writepagecache();
  1487. if (err)
  1488. goto out1;
  1489. err = nfs_init_directcache();
  1490. if (err)
  1491. goto out0;
  1492. #ifdef CONFIG_PROC_FS
  1493. rpc_proc_register(&init_net, &nfs_rpcstat);
  1494. #endif
  1495. if ((err = register_nfs_fs()) != 0)
  1496. goto out;
  1497. return 0;
  1498. out:
  1499. #ifdef CONFIG_PROC_FS
  1500. rpc_proc_unregister(&init_net, "nfs");
  1501. #endif
  1502. nfs_destroy_directcache();
  1503. out0:
  1504. nfs_destroy_writepagecache();
  1505. out1:
  1506. nfs_destroy_readpagecache();
  1507. out2:
  1508. nfs_destroy_inodecache();
  1509. out3:
  1510. nfs_destroy_nfspagecache();
  1511. out4:
  1512. nfs_fs_proc_exit();
  1513. out5:
  1514. nfsiod_stop();
  1515. out6:
  1516. nfs_fscache_unregister();
  1517. out7:
  1518. unregister_pernet_subsys(&nfs_net_ops);
  1519. out8:
  1520. nfs_dns_resolver_destroy();
  1521. out9:
  1522. nfs_idmap_quit();
  1523. out10:
  1524. return err;
  1525. }
  1526. static void __exit exit_nfs_fs(void)
  1527. {
  1528. nfs_destroy_directcache();
  1529. nfs_destroy_writepagecache();
  1530. nfs_destroy_readpagecache();
  1531. nfs_destroy_inodecache();
  1532. nfs_destroy_nfspagecache();
  1533. nfs_fscache_unregister();
  1534. unregister_pernet_subsys(&nfs_net_ops);
  1535. nfs_dns_resolver_destroy();
  1536. nfs_idmap_quit();
  1537. #ifdef CONFIG_PROC_FS
  1538. rpc_proc_unregister(&init_net, "nfs");
  1539. #endif
  1540. unregister_nfs_fs();
  1541. nfs_fs_proc_exit();
  1542. nfsiod_stop();
  1543. }
  1544. /* Not quite true; I just maintain it */
  1545. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  1546. MODULE_LICENSE("GPL");
  1547. module_param(enable_ino64, bool, 0644);
  1548. module_init(init_nfs_fs)
  1549. module_exit(exit_nfs_fs)