vfs.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  1. #define MSNFS /* HACK HACK */
  2. /*
  3. * linux/fs/nfsd/vfs.c
  4. *
  5. * File operations used by nfsd. Some of these have been ripped from
  6. * other parts of the kernel because they weren't exported, others
  7. * are partial duplicates with added or changed functionality.
  8. *
  9. * Note that several functions dget() the dentry upon which they want
  10. * to act, most notably those that create directory entries. Response
  11. * dentry's are dput()'d if necessary in the release callback.
  12. * So if you notice code paths that apparently fail to dput() the
  13. * dentry, don't worry--they have been taken care of.
  14. *
  15. * Copyright (C) 1995-1999 Olaf Kirch <okir@monad.swb.de>
  16. * Zerocpy NFS support (C) 2002 Hirokazu Takahashi <taka@valinux.co.jp>
  17. */
  18. #include <linux/string.h>
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/fs.h>
  22. #include <linux/file.h>
  23. #include <linux/mount.h>
  24. #include <linux/major.h>
  25. #include <linux/splice.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/stat.h>
  28. #include <linux/fcntl.h>
  29. #include <linux/net.h>
  30. #include <linux/unistd.h>
  31. #include <linux/slab.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/in.h>
  34. #include <linux/module.h>
  35. #include <linux/namei.h>
  36. #include <linux/vfs.h>
  37. #include <linux/delay.h>
  38. #include <linux/sunrpc/svc.h>
  39. #include <linux/nfsd/nfsd.h>
  40. #ifdef CONFIG_NFSD_V3
  41. #include <linux/nfs3.h>
  42. #include <linux/nfsd/xdr3.h>
  43. #endif /* CONFIG_NFSD_V3 */
  44. #include <linux/nfsd/nfsfh.h>
  45. #include <linux/quotaops.h>
  46. #include <linux/fsnotify.h>
  47. #include <linux/posix_acl.h>
  48. #include <linux/posix_acl_xattr.h>
  49. #include <linux/xattr.h>
  50. #ifdef CONFIG_NFSD_V4
  51. #include <linux/nfs4.h>
  52. #include <linux/nfs4_acl.h>
  53. #include <linux/nfsd_idmap.h>
  54. #include <linux/security.h>
  55. #endif /* CONFIG_NFSD_V4 */
  56. #include <linux/jhash.h>
  57. #include <asm/uaccess.h>
  58. #define NFSDDBG_FACILITY NFSDDBG_FILEOP
  59. /*
  60. * This is a cache of readahead params that help us choose the proper
  61. * readahead strategy. Initially, we set all readahead parameters to 0
  62. * and let the VFS handle things.
  63. * If you increase the number of cached files very much, you'll need to
  64. * add a hash table here.
  65. */
  66. struct raparms {
  67. struct raparms *p_next;
  68. unsigned int p_count;
  69. ino_t p_ino;
  70. dev_t p_dev;
  71. int p_set;
  72. struct file_ra_state p_ra;
  73. unsigned int p_hindex;
  74. };
  75. struct raparm_hbucket {
  76. struct raparms *pb_head;
  77. spinlock_t pb_lock;
  78. } ____cacheline_aligned_in_smp;
  79. #define RAPARM_HASH_BITS 4
  80. #define RAPARM_HASH_SIZE (1<<RAPARM_HASH_BITS)
  81. #define RAPARM_HASH_MASK (RAPARM_HASH_SIZE-1)
  82. static struct raparm_hbucket raparm_hash[RAPARM_HASH_SIZE];
  83. /*
  84. * Called from nfsd_lookup and encode_dirent. Check if we have crossed
  85. * a mount point.
  86. * Returns -EAGAIN or -ETIMEDOUT leaving *dpp and *expp unchanged,
  87. * or nfs_ok having possibly changed *dpp and *expp
  88. */
  89. int
  90. nfsd_cross_mnt(struct svc_rqst *rqstp, struct dentry **dpp,
  91. struct svc_export **expp)
  92. {
  93. struct svc_export *exp = *expp, *exp2 = NULL;
  94. struct dentry *dentry = *dpp;
  95. struct vfsmount *mnt = mntget(exp->ex_path.mnt);
  96. struct dentry *mounts = dget(dentry);
  97. int err = 0;
  98. while (follow_down(&mnt,&mounts)&&d_mountpoint(mounts));
  99. exp2 = rqst_exp_get_by_name(rqstp, mnt, mounts);
  100. if (IS_ERR(exp2)) {
  101. if (PTR_ERR(exp2) != -ENOENT)
  102. err = PTR_ERR(exp2);
  103. dput(mounts);
  104. mntput(mnt);
  105. goto out;
  106. }
  107. if ((exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  108. /* successfully crossed mount point */
  109. exp_put(exp);
  110. *expp = exp2;
  111. dput(dentry);
  112. *dpp = mounts;
  113. } else {
  114. exp_put(exp2);
  115. dput(mounts);
  116. }
  117. mntput(mnt);
  118. out:
  119. return err;
  120. }
  121. __be32
  122. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  123. const char *name, unsigned int len,
  124. struct svc_export **exp_ret, struct dentry **dentry_ret)
  125. {
  126. struct svc_export *exp;
  127. struct dentry *dparent;
  128. struct dentry *dentry;
  129. __be32 err;
  130. int host_err;
  131. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  132. /* Obtain dentry and export. */
  133. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  134. if (err)
  135. return err;
  136. dparent = fhp->fh_dentry;
  137. exp = fhp->fh_export;
  138. exp_get(exp);
  139. /* Lookup the name, but don't follow links */
  140. if (isdotent(name, len)) {
  141. if (len==1)
  142. dentry = dget(dparent);
  143. else if (dparent != exp->ex_path.dentry)
  144. dentry = dget_parent(dparent);
  145. else if (!EX_NOHIDE(exp))
  146. dentry = dget(dparent); /* .. == . just like at / */
  147. else {
  148. /* checking mountpoint crossing is very different when stepping up */
  149. struct svc_export *exp2 = NULL;
  150. struct dentry *dp;
  151. struct vfsmount *mnt = mntget(exp->ex_path.mnt);
  152. dentry = dget(dparent);
  153. while(dentry == mnt->mnt_root && follow_up(&mnt, &dentry))
  154. ;
  155. dp = dget_parent(dentry);
  156. dput(dentry);
  157. dentry = dp;
  158. exp2 = rqst_exp_parent(rqstp, mnt, dentry);
  159. if (PTR_ERR(exp2) == -ENOENT) {
  160. dput(dentry);
  161. dentry = dget(dparent);
  162. } else if (IS_ERR(exp2)) {
  163. host_err = PTR_ERR(exp2);
  164. dput(dentry);
  165. mntput(mnt);
  166. goto out_nfserr;
  167. } else {
  168. exp_put(exp);
  169. exp = exp2;
  170. }
  171. mntput(mnt);
  172. }
  173. } else {
  174. fh_lock(fhp);
  175. dentry = lookup_one_len(name, dparent, len);
  176. host_err = PTR_ERR(dentry);
  177. if (IS_ERR(dentry))
  178. goto out_nfserr;
  179. /*
  180. * check if we have crossed a mount point ...
  181. */
  182. if (d_mountpoint(dentry)) {
  183. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  184. dput(dentry);
  185. goto out_nfserr;
  186. }
  187. }
  188. }
  189. *dentry_ret = dentry;
  190. *exp_ret = exp;
  191. return 0;
  192. out_nfserr:
  193. exp_put(exp);
  194. return nfserrno(host_err);
  195. }
  196. /*
  197. * Look up one component of a pathname.
  198. * N.B. After this call _both_ fhp and resfh need an fh_put
  199. *
  200. * If the lookup would cross a mountpoint, and the mounted filesystem
  201. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  202. * accepted as it stands and the mounted directory is
  203. * returned. Otherwise the covered directory is returned.
  204. * NOTE: this mountpoint crossing is not supported properly by all
  205. * clients and is explicitly disallowed for NFSv3
  206. * NeilBrown <neilb@cse.unsw.edu.au>
  207. */
  208. __be32
  209. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  210. unsigned int len, struct svc_fh *resfh)
  211. {
  212. struct svc_export *exp;
  213. struct dentry *dentry;
  214. __be32 err;
  215. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  216. if (err)
  217. return err;
  218. err = check_nfsd_access(exp, rqstp);
  219. if (err)
  220. goto out;
  221. /*
  222. * Note: we compose the file handle now, but as the
  223. * dentry may be negative, it may need to be updated.
  224. */
  225. err = fh_compose(resfh, exp, dentry, fhp);
  226. if (!err && !dentry->d_inode)
  227. err = nfserr_noent;
  228. out:
  229. dput(dentry);
  230. exp_put(exp);
  231. return err;
  232. }
  233. /*
  234. * Set various file attributes.
  235. * N.B. After this call fhp needs an fh_put
  236. */
  237. __be32
  238. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  239. int check_guard, time_t guardtime)
  240. {
  241. struct dentry *dentry;
  242. struct inode *inode;
  243. int accmode = NFSD_MAY_SATTR;
  244. int ftype = 0;
  245. __be32 err;
  246. int host_err;
  247. int size_change = 0;
  248. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
  249. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  250. if (iap->ia_valid & ATTR_SIZE)
  251. ftype = S_IFREG;
  252. /* Get inode */
  253. err = fh_verify(rqstp, fhp, ftype, accmode);
  254. if (err)
  255. goto out;
  256. dentry = fhp->fh_dentry;
  257. inode = dentry->d_inode;
  258. /* Ignore any mode updates on symlinks */
  259. if (S_ISLNK(inode->i_mode))
  260. iap->ia_valid &= ~ATTR_MODE;
  261. if (!iap->ia_valid)
  262. goto out;
  263. /*
  264. * NFSv2 does not differentiate between "set-[ac]time-to-now"
  265. * which only requires access, and "set-[ac]time-to-X" which
  266. * requires ownership.
  267. * So if it looks like it might be "set both to the same time which
  268. * is close to now", and if inode_change_ok fails, then we
  269. * convert to "set to now" instead of "set to explicit time"
  270. *
  271. * We only call inode_change_ok as the last test as technically
  272. * it is not an interface that we should be using. It is only
  273. * valid if the filesystem does not define it's own i_op->setattr.
  274. */
  275. #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
  276. #define MAX_TOUCH_TIME_ERROR (30*60)
  277. if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
  278. iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
  279. /*
  280. * Looks probable.
  281. *
  282. * Now just make sure time is in the right ballpark.
  283. * Solaris, at least, doesn't seem to care what the time
  284. * request is. We require it be within 30 minutes of now.
  285. */
  286. time_t delta = iap->ia_atime.tv_sec - get_seconds();
  287. if (delta < 0)
  288. delta = -delta;
  289. if (delta < MAX_TOUCH_TIME_ERROR &&
  290. inode_change_ok(inode, iap) != 0) {
  291. /*
  292. * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
  293. * This will cause notify_change to set these times
  294. * to "now"
  295. */
  296. iap->ia_valid &= ~BOTH_TIME_SET;
  297. }
  298. }
  299. /*
  300. * The size case is special.
  301. * It changes the file as well as the attributes.
  302. */
  303. if (iap->ia_valid & ATTR_SIZE) {
  304. if (iap->ia_size < inode->i_size) {
  305. err = nfsd_permission(rqstp, fhp->fh_export, dentry,
  306. NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
  307. if (err)
  308. goto out;
  309. }
  310. /*
  311. * If we are changing the size of the file, then
  312. * we need to break all leases.
  313. */
  314. host_err = break_lease(inode, FMODE_WRITE | O_NONBLOCK);
  315. if (host_err == -EWOULDBLOCK)
  316. host_err = -ETIMEDOUT;
  317. if (host_err) /* ENOMEM or EWOULDBLOCK */
  318. goto out_nfserr;
  319. host_err = get_write_access(inode);
  320. if (host_err)
  321. goto out_nfserr;
  322. size_change = 1;
  323. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  324. if (host_err) {
  325. put_write_access(inode);
  326. goto out_nfserr;
  327. }
  328. DQUOT_INIT(inode);
  329. }
  330. /* sanitize the mode change */
  331. if (iap->ia_valid & ATTR_MODE) {
  332. iap->ia_mode &= S_IALLUGO;
  333. iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
  334. }
  335. /* Revoke setuid/setgid on chown */
  336. if (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
  337. ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)) {
  338. iap->ia_valid |= ATTR_KILL_PRIV;
  339. if (iap->ia_valid & ATTR_MODE) {
  340. /* we're setting mode too, just clear the s*id bits */
  341. iap->ia_mode &= ~S_ISUID;
  342. if (iap->ia_mode & S_IXGRP)
  343. iap->ia_mode &= ~S_ISGID;
  344. } else {
  345. /* set ATTR_KILL_* bits and let VFS handle it */
  346. iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
  347. }
  348. }
  349. /* Change the attributes. */
  350. iap->ia_valid |= ATTR_CTIME;
  351. err = nfserr_notsync;
  352. if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
  353. fh_lock(fhp);
  354. host_err = notify_change(dentry, iap);
  355. err = nfserrno(host_err);
  356. fh_unlock(fhp);
  357. }
  358. if (size_change)
  359. put_write_access(inode);
  360. if (!err)
  361. if (EX_ISSYNC(fhp->fh_export))
  362. write_inode_now(inode, 1);
  363. out:
  364. return err;
  365. out_nfserr:
  366. err = nfserrno(host_err);
  367. goto out;
  368. }
  369. #if defined(CONFIG_NFSD_V2_ACL) || \
  370. defined(CONFIG_NFSD_V3_ACL) || \
  371. defined(CONFIG_NFSD_V4)
  372. static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
  373. {
  374. ssize_t buflen;
  375. ssize_t ret;
  376. buflen = vfs_getxattr(dentry, key, NULL, 0);
  377. if (buflen <= 0)
  378. return buflen;
  379. *buf = kmalloc(buflen, GFP_KERNEL);
  380. if (!*buf)
  381. return -ENOMEM;
  382. ret = vfs_getxattr(dentry, key, *buf, buflen);
  383. if (ret < 0)
  384. kfree(*buf);
  385. return ret;
  386. }
  387. #endif
  388. #if defined(CONFIG_NFSD_V4)
  389. static int
  390. set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
  391. {
  392. int len;
  393. size_t buflen;
  394. char *buf = NULL;
  395. int error = 0;
  396. buflen = posix_acl_xattr_size(pacl->a_count);
  397. buf = kmalloc(buflen, GFP_KERNEL);
  398. error = -ENOMEM;
  399. if (buf == NULL)
  400. goto out;
  401. len = posix_acl_to_xattr(pacl, buf, buflen);
  402. if (len < 0) {
  403. error = len;
  404. goto out;
  405. }
  406. error = vfs_setxattr(dentry, key, buf, len, 0);
  407. out:
  408. kfree(buf);
  409. return error;
  410. }
  411. __be32
  412. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  413. struct nfs4_acl *acl)
  414. {
  415. __be32 error;
  416. int host_error;
  417. struct dentry *dentry;
  418. struct inode *inode;
  419. struct posix_acl *pacl = NULL, *dpacl = NULL;
  420. unsigned int flags = 0;
  421. /* Get inode */
  422. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
  423. if (error)
  424. return error;
  425. dentry = fhp->fh_dentry;
  426. inode = dentry->d_inode;
  427. if (S_ISDIR(inode->i_mode))
  428. flags = NFS4_ACL_DIR;
  429. host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  430. if (host_error == -EINVAL) {
  431. return nfserr_attrnotsupp;
  432. } else if (host_error < 0)
  433. goto out_nfserr;
  434. host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
  435. if (host_error < 0)
  436. goto out_release;
  437. if (S_ISDIR(inode->i_mode))
  438. host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
  439. out_release:
  440. posix_acl_release(pacl);
  441. posix_acl_release(dpacl);
  442. out_nfserr:
  443. if (host_error == -EOPNOTSUPP)
  444. return nfserr_attrnotsupp;
  445. else
  446. return nfserrno(host_error);
  447. }
  448. static struct posix_acl *
  449. _get_posix_acl(struct dentry *dentry, char *key)
  450. {
  451. void *buf = NULL;
  452. struct posix_acl *pacl = NULL;
  453. int buflen;
  454. buflen = nfsd_getxattr(dentry, key, &buf);
  455. if (!buflen)
  456. buflen = -ENODATA;
  457. if (buflen <= 0)
  458. return ERR_PTR(buflen);
  459. pacl = posix_acl_from_xattr(buf, buflen);
  460. kfree(buf);
  461. return pacl;
  462. }
  463. int
  464. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
  465. {
  466. struct inode *inode = dentry->d_inode;
  467. int error = 0;
  468. struct posix_acl *pacl = NULL, *dpacl = NULL;
  469. unsigned int flags = 0;
  470. pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
  471. if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
  472. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  473. if (IS_ERR(pacl)) {
  474. error = PTR_ERR(pacl);
  475. pacl = NULL;
  476. goto out;
  477. }
  478. if (S_ISDIR(inode->i_mode)) {
  479. dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
  480. if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
  481. dpacl = NULL;
  482. else if (IS_ERR(dpacl)) {
  483. error = PTR_ERR(dpacl);
  484. dpacl = NULL;
  485. goto out;
  486. }
  487. flags = NFS4_ACL_DIR;
  488. }
  489. *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
  490. if (IS_ERR(*acl)) {
  491. error = PTR_ERR(*acl);
  492. *acl = NULL;
  493. }
  494. out:
  495. posix_acl_release(pacl);
  496. posix_acl_release(dpacl);
  497. return error;
  498. }
  499. #endif /* defined(CONFIG_NFS_V4) */
  500. #ifdef CONFIG_NFSD_V3
  501. /*
  502. * Check server access rights to a file system object
  503. */
  504. struct accessmap {
  505. u32 access;
  506. int how;
  507. };
  508. static struct accessmap nfs3_regaccess[] = {
  509. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  510. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  511. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
  512. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
  513. { 0, 0 }
  514. };
  515. static struct accessmap nfs3_diraccess[] = {
  516. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  517. { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
  518. { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
  519. { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
  520. { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
  521. { 0, 0 }
  522. };
  523. static struct accessmap nfs3_anyaccess[] = {
  524. /* Some clients - Solaris 2.6 at least, make an access call
  525. * to the server to check for access for things like /dev/null
  526. * (which really, the server doesn't care about). So
  527. * We provide simple access checking for them, looking
  528. * mainly at mode bits, and we make sure to ignore read-only
  529. * filesystem checks
  530. */
  531. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  532. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  533. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  534. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  535. { 0, 0 }
  536. };
  537. __be32
  538. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  539. {
  540. struct accessmap *map;
  541. struct svc_export *export;
  542. struct dentry *dentry;
  543. u32 query, result = 0, sresult = 0;
  544. __be32 error;
  545. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  546. if (error)
  547. goto out;
  548. export = fhp->fh_export;
  549. dentry = fhp->fh_dentry;
  550. if (S_ISREG(dentry->d_inode->i_mode))
  551. map = nfs3_regaccess;
  552. else if (S_ISDIR(dentry->d_inode->i_mode))
  553. map = nfs3_diraccess;
  554. else
  555. map = nfs3_anyaccess;
  556. query = *access;
  557. for (; map->access; map++) {
  558. if (map->access & query) {
  559. __be32 err2;
  560. sresult |= map->access;
  561. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  562. switch (err2) {
  563. case nfs_ok:
  564. result |= map->access;
  565. break;
  566. /* the following error codes just mean the access was not allowed,
  567. * rather than an error occurred */
  568. case nfserr_rofs:
  569. case nfserr_acces:
  570. case nfserr_perm:
  571. /* simply don't "or" in the access bit. */
  572. break;
  573. default:
  574. error = err2;
  575. goto out;
  576. }
  577. }
  578. }
  579. *access = result;
  580. if (supported)
  581. *supported = sresult;
  582. out:
  583. return error;
  584. }
  585. #endif /* CONFIG_NFSD_V3 */
  586. /*
  587. * Open an existing file or directory.
  588. * The access argument indicates the type of open (read/write/lock)
  589. * N.B. After this call fhp needs an fh_put
  590. */
  591. __be32
  592. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  593. int access, struct file **filp)
  594. {
  595. const struct cred *cred = current_cred();
  596. struct dentry *dentry;
  597. struct inode *inode;
  598. int flags = O_RDONLY|O_LARGEFILE;
  599. __be32 err;
  600. int host_err;
  601. /*
  602. * If we get here, then the client has already done an "open",
  603. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  604. * in case a chmod has now revoked permission.
  605. */
  606. err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
  607. if (err)
  608. goto out;
  609. dentry = fhp->fh_dentry;
  610. inode = dentry->d_inode;
  611. /* Disallow write access to files with the append-only bit set
  612. * or any access when mandatory locking enabled
  613. */
  614. err = nfserr_perm;
  615. if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
  616. goto out;
  617. /*
  618. * We must ignore files (but only files) which might have mandatory
  619. * locks on them because there is no way to know if the accesser has
  620. * the lock.
  621. */
  622. if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
  623. goto out;
  624. if (!inode->i_fop)
  625. goto out;
  626. /*
  627. * Check to see if there are any leases on this file.
  628. * This may block while leases are broken.
  629. */
  630. host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? FMODE_WRITE : 0));
  631. if (host_err == -EWOULDBLOCK)
  632. host_err = -ETIMEDOUT;
  633. if (host_err) /* NOMEM or WOULDBLOCK */
  634. goto out_nfserr;
  635. if (access & NFSD_MAY_WRITE) {
  636. if (access & NFSD_MAY_READ)
  637. flags = O_RDWR|O_LARGEFILE;
  638. else
  639. flags = O_WRONLY|O_LARGEFILE;
  640. DQUOT_INIT(inode);
  641. }
  642. *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
  643. flags, cred);
  644. if (IS_ERR(*filp))
  645. host_err = PTR_ERR(*filp);
  646. out_nfserr:
  647. err = nfserrno(host_err);
  648. out:
  649. return err;
  650. }
  651. /*
  652. * Close a file.
  653. */
  654. void
  655. nfsd_close(struct file *filp)
  656. {
  657. fput(filp);
  658. }
  659. /*
  660. * Sync a file
  661. * As this calls fsync (not fdatasync) there is no need for a write_inode
  662. * after it.
  663. */
  664. static inline int nfsd_dosync(struct file *filp, struct dentry *dp,
  665. const struct file_operations *fop)
  666. {
  667. struct inode *inode = dp->d_inode;
  668. int (*fsync) (struct file *, struct dentry *, int);
  669. int err;
  670. err = filemap_fdatawrite(inode->i_mapping);
  671. if (err == 0 && fop && (fsync = fop->fsync))
  672. err = fsync(filp, dp, 0);
  673. if (err == 0)
  674. err = filemap_fdatawait(inode->i_mapping);
  675. return err;
  676. }
  677. static int
  678. nfsd_sync(struct file *filp)
  679. {
  680. int err;
  681. struct inode *inode = filp->f_path.dentry->d_inode;
  682. dprintk("nfsd: sync file %s\n", filp->f_path.dentry->d_name.name);
  683. mutex_lock(&inode->i_mutex);
  684. err=nfsd_dosync(filp, filp->f_path.dentry, filp->f_op);
  685. mutex_unlock(&inode->i_mutex);
  686. return err;
  687. }
  688. int
  689. nfsd_sync_dir(struct dentry *dp)
  690. {
  691. return nfsd_dosync(NULL, dp, dp->d_inode->i_fop);
  692. }
  693. /*
  694. * Obtain the readahead parameters for the file
  695. * specified by (dev, ino).
  696. */
  697. static inline struct raparms *
  698. nfsd_get_raparms(dev_t dev, ino_t ino)
  699. {
  700. struct raparms *ra, **rap, **frap = NULL;
  701. int depth = 0;
  702. unsigned int hash;
  703. struct raparm_hbucket *rab;
  704. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  705. rab = &raparm_hash[hash];
  706. spin_lock(&rab->pb_lock);
  707. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  708. if (ra->p_ino == ino && ra->p_dev == dev)
  709. goto found;
  710. depth++;
  711. if (ra->p_count == 0)
  712. frap = rap;
  713. }
  714. depth = nfsdstats.ra_size*11/10;
  715. if (!frap) {
  716. spin_unlock(&rab->pb_lock);
  717. return NULL;
  718. }
  719. rap = frap;
  720. ra = *frap;
  721. ra->p_dev = dev;
  722. ra->p_ino = ino;
  723. ra->p_set = 0;
  724. ra->p_hindex = hash;
  725. found:
  726. if (rap != &rab->pb_head) {
  727. *rap = ra->p_next;
  728. ra->p_next = rab->pb_head;
  729. rab->pb_head = ra;
  730. }
  731. ra->p_count++;
  732. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  733. spin_unlock(&rab->pb_lock);
  734. return ra;
  735. }
  736. /*
  737. * Grab and keep cached pages associated with a file in the svc_rqst
  738. * so that they can be passed to the network sendmsg/sendpage routines
  739. * directly. They will be released after the sending has completed.
  740. */
  741. static int
  742. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  743. struct splice_desc *sd)
  744. {
  745. struct svc_rqst *rqstp = sd->u.data;
  746. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  747. struct page *page = buf->page;
  748. size_t size;
  749. int ret;
  750. ret = buf->ops->confirm(pipe, buf);
  751. if (unlikely(ret))
  752. return ret;
  753. size = sd->len;
  754. if (rqstp->rq_res.page_len == 0) {
  755. get_page(page);
  756. put_page(*pp);
  757. *pp = page;
  758. rqstp->rq_resused++;
  759. rqstp->rq_res.page_base = buf->offset;
  760. rqstp->rq_res.page_len = size;
  761. } else if (page != pp[-1]) {
  762. get_page(page);
  763. if (*pp)
  764. put_page(*pp);
  765. *pp = page;
  766. rqstp->rq_resused++;
  767. rqstp->rq_res.page_len += size;
  768. } else
  769. rqstp->rq_res.page_len += size;
  770. return size;
  771. }
  772. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  773. struct splice_desc *sd)
  774. {
  775. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  776. }
  777. static inline int svc_msnfs(struct svc_fh *ffhp)
  778. {
  779. #ifdef MSNFS
  780. return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
  781. #else
  782. return 0;
  783. #endif
  784. }
  785. static __be32
  786. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  787. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  788. {
  789. struct inode *inode;
  790. struct raparms *ra;
  791. mm_segment_t oldfs;
  792. __be32 err;
  793. int host_err;
  794. err = nfserr_perm;
  795. inode = file->f_path.dentry->d_inode;
  796. if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
  797. goto out;
  798. /* Get readahead parameters */
  799. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  800. if (ra && ra->p_set)
  801. file->f_ra = ra->p_ra;
  802. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  803. struct splice_desc sd = {
  804. .len = 0,
  805. .total_len = *count,
  806. .pos = offset,
  807. .u.data = rqstp,
  808. };
  809. rqstp->rq_resused = 1;
  810. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  811. } else {
  812. oldfs = get_fs();
  813. set_fs(KERNEL_DS);
  814. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  815. set_fs(oldfs);
  816. }
  817. /* Write back readahead params */
  818. if (ra) {
  819. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  820. spin_lock(&rab->pb_lock);
  821. ra->p_ra = file->f_ra;
  822. ra->p_set = 1;
  823. ra->p_count--;
  824. spin_unlock(&rab->pb_lock);
  825. }
  826. if (host_err >= 0) {
  827. nfsdstats.io_read += host_err;
  828. *count = host_err;
  829. err = 0;
  830. fsnotify_access(file->f_path.dentry);
  831. } else
  832. err = nfserrno(host_err);
  833. out:
  834. return err;
  835. }
  836. static void kill_suid(struct dentry *dentry)
  837. {
  838. struct iattr ia;
  839. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  840. mutex_lock(&dentry->d_inode->i_mutex);
  841. notify_change(dentry, &ia);
  842. mutex_unlock(&dentry->d_inode->i_mutex);
  843. }
  844. static __be32
  845. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  846. loff_t offset, struct kvec *vec, int vlen,
  847. unsigned long cnt, int *stablep)
  848. {
  849. struct svc_export *exp;
  850. struct dentry *dentry;
  851. struct inode *inode;
  852. mm_segment_t oldfs;
  853. __be32 err = 0;
  854. int host_err;
  855. int stable = *stablep;
  856. #ifdef MSNFS
  857. err = nfserr_perm;
  858. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  859. (!lock_may_write(file->f_path.dentry->d_inode, offset, cnt)))
  860. goto out;
  861. #endif
  862. dentry = file->f_path.dentry;
  863. inode = dentry->d_inode;
  864. exp = fhp->fh_export;
  865. /*
  866. * Request sync writes if
  867. * - the sync export option has been set, or
  868. * - the client requested O_SYNC behavior (NFSv3 feature).
  869. * - The file system doesn't support fsync().
  870. * When gathered writes have been configured for this volume,
  871. * flushing the data to disk is handled separately below.
  872. */
  873. if (!file->f_op->fsync) {/* COMMIT3 cannot work */
  874. stable = 2;
  875. *stablep = 2; /* FILE_SYNC */
  876. }
  877. if (!EX_ISSYNC(exp))
  878. stable = 0;
  879. if (stable && !EX_WGATHER(exp)) {
  880. spin_lock(&file->f_lock);
  881. file->f_flags |= O_SYNC;
  882. spin_unlock(&file->f_lock);
  883. }
  884. /* Write the data. */
  885. oldfs = get_fs(); set_fs(KERNEL_DS);
  886. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  887. set_fs(oldfs);
  888. if (host_err >= 0) {
  889. nfsdstats.io_write += cnt;
  890. fsnotify_modify(file->f_path.dentry);
  891. }
  892. /* clear setuid/setgid flag after write */
  893. if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
  894. kill_suid(dentry);
  895. if (host_err >= 0 && stable) {
  896. static ino_t last_ino;
  897. static dev_t last_dev;
  898. /*
  899. * Gathered writes: If another process is currently
  900. * writing to the file, there's a high chance
  901. * this is another nfsd (triggered by a bulk write
  902. * from a client's biod). Rather than syncing the
  903. * file with each write request, we sleep for 10 msec.
  904. *
  905. * I don't know if this roughly approximates
  906. * C. Juszak's idea of gathered writes, but it's a
  907. * nice and simple solution (IMHO), and it seems to
  908. * work:-)
  909. */
  910. if (EX_WGATHER(exp)) {
  911. if (atomic_read(&inode->i_writecount) > 1
  912. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  913. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  914. msleep(10);
  915. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  916. }
  917. if (inode->i_state & I_DIRTY) {
  918. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  919. host_err=nfsd_sync(file);
  920. }
  921. #if 0
  922. wake_up(&inode->i_wait);
  923. #endif
  924. }
  925. last_ino = inode->i_ino;
  926. last_dev = inode->i_sb->s_dev;
  927. }
  928. dprintk("nfsd: write complete host_err=%d\n", host_err);
  929. if (host_err >= 0)
  930. err = 0;
  931. else
  932. err = nfserrno(host_err);
  933. out:
  934. return err;
  935. }
  936. /*
  937. * Read data from a file. count must contain the requested read count
  938. * on entry. On return, *count contains the number of bytes actually read.
  939. * N.B. After this call fhp needs an fh_put
  940. */
  941. __be32
  942. nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  943. loff_t offset, struct kvec *vec, int vlen,
  944. unsigned long *count)
  945. {
  946. __be32 err;
  947. if (file) {
  948. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  949. NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
  950. if (err)
  951. goto out;
  952. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  953. } else {
  954. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  955. if (err)
  956. goto out;
  957. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  958. nfsd_close(file);
  959. }
  960. out:
  961. return err;
  962. }
  963. /*
  964. * Write data to a file.
  965. * The stable flag requests synchronous writes.
  966. * N.B. After this call fhp needs an fh_put
  967. */
  968. __be32
  969. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  970. loff_t offset, struct kvec *vec, int vlen, unsigned long cnt,
  971. int *stablep)
  972. {
  973. __be32 err = 0;
  974. if (file) {
  975. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  976. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  977. if (err)
  978. goto out;
  979. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  980. stablep);
  981. } else {
  982. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  983. if (err)
  984. goto out;
  985. if (cnt)
  986. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  987. cnt, stablep);
  988. nfsd_close(file);
  989. }
  990. out:
  991. return err;
  992. }
  993. #ifdef CONFIG_NFSD_V3
  994. /*
  995. * Commit all pending writes to stable storage.
  996. * Strictly speaking, we could sync just the indicated file region here,
  997. * but there's currently no way we can ask the VFS to do so.
  998. *
  999. * Unfortunately we cannot lock the file to make sure we return full WCC
  1000. * data to the client, as locking happens lower down in the filesystem.
  1001. */
  1002. __be32
  1003. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1004. loff_t offset, unsigned long count)
  1005. {
  1006. struct file *file;
  1007. __be32 err;
  1008. if ((u64)count > ~(u64)offset)
  1009. return nfserr_inval;
  1010. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  1011. if (err)
  1012. return err;
  1013. if (EX_ISSYNC(fhp->fh_export)) {
  1014. if (file->f_op && file->f_op->fsync) {
  1015. err = nfserrno(nfsd_sync(file));
  1016. } else {
  1017. err = nfserr_notsupp;
  1018. }
  1019. }
  1020. nfsd_close(file);
  1021. return err;
  1022. }
  1023. #endif /* CONFIG_NFSD_V3 */
  1024. static __be32
  1025. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1026. struct iattr *iap)
  1027. {
  1028. /*
  1029. * Mode has already been set earlier in create:
  1030. */
  1031. iap->ia_valid &= ~ATTR_MODE;
  1032. /*
  1033. * Setting uid/gid works only for root. Irix appears to
  1034. * send along the gid on create when it tries to implement
  1035. * setgid directories via NFS:
  1036. */
  1037. if (current_fsuid() != 0)
  1038. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1039. if (iap->ia_valid)
  1040. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1041. return 0;
  1042. }
  1043. /*
  1044. * Create a file (regular, directory, device, fifo); UNIX sockets
  1045. * not yet implemented.
  1046. * If the response fh has been verified, the parent directory should
  1047. * already be locked. Note that the parent directory is left locked.
  1048. *
  1049. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1050. */
  1051. __be32
  1052. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1053. char *fname, int flen, struct iattr *iap,
  1054. int type, dev_t rdev, struct svc_fh *resfhp)
  1055. {
  1056. struct dentry *dentry, *dchild = NULL;
  1057. struct inode *dirp;
  1058. __be32 err;
  1059. __be32 err2;
  1060. int host_err;
  1061. err = nfserr_perm;
  1062. if (!flen)
  1063. goto out;
  1064. err = nfserr_exist;
  1065. if (isdotent(fname, flen))
  1066. goto out;
  1067. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1068. if (err)
  1069. goto out;
  1070. dentry = fhp->fh_dentry;
  1071. dirp = dentry->d_inode;
  1072. err = nfserr_notdir;
  1073. if (!dirp->i_op->lookup)
  1074. goto out;
  1075. /*
  1076. * Check whether the response file handle has been verified yet.
  1077. * If it has, the parent directory should already be locked.
  1078. */
  1079. if (!resfhp->fh_dentry) {
  1080. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1081. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1082. dchild = lookup_one_len(fname, dentry, flen);
  1083. host_err = PTR_ERR(dchild);
  1084. if (IS_ERR(dchild))
  1085. goto out_nfserr;
  1086. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1087. if (err)
  1088. goto out;
  1089. } else {
  1090. /* called from nfsd_proc_create */
  1091. dchild = dget(resfhp->fh_dentry);
  1092. if (!fhp->fh_locked) {
  1093. /* not actually possible */
  1094. printk(KERN_ERR
  1095. "nfsd_create: parent %s/%s not locked!\n",
  1096. dentry->d_parent->d_name.name,
  1097. dentry->d_name.name);
  1098. err = nfserr_io;
  1099. goto out;
  1100. }
  1101. }
  1102. /*
  1103. * Make sure the child dentry is still negative ...
  1104. */
  1105. err = nfserr_exist;
  1106. if (dchild->d_inode) {
  1107. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1108. dentry->d_name.name, dchild->d_name.name);
  1109. goto out;
  1110. }
  1111. if (!(iap->ia_valid & ATTR_MODE))
  1112. iap->ia_mode = 0;
  1113. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1114. err = nfserr_inval;
  1115. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1116. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1117. type);
  1118. goto out;
  1119. }
  1120. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1121. if (host_err)
  1122. goto out_nfserr;
  1123. /*
  1124. * Get the dir op function pointer.
  1125. */
  1126. err = 0;
  1127. switch (type) {
  1128. case S_IFREG:
  1129. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1130. break;
  1131. case S_IFDIR:
  1132. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1133. break;
  1134. case S_IFCHR:
  1135. case S_IFBLK:
  1136. case S_IFIFO:
  1137. case S_IFSOCK:
  1138. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1139. break;
  1140. }
  1141. if (host_err < 0) {
  1142. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1143. goto out_nfserr;
  1144. }
  1145. if (EX_ISSYNC(fhp->fh_export)) {
  1146. err = nfserrno(nfsd_sync_dir(dentry));
  1147. write_inode_now(dchild->d_inode, 1);
  1148. }
  1149. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1150. if (err2)
  1151. err = err2;
  1152. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1153. /*
  1154. * Update the file handle to get the new inode info.
  1155. */
  1156. if (!err)
  1157. err = fh_update(resfhp);
  1158. out:
  1159. if (dchild && !IS_ERR(dchild))
  1160. dput(dchild);
  1161. return err;
  1162. out_nfserr:
  1163. err = nfserrno(host_err);
  1164. goto out;
  1165. }
  1166. #ifdef CONFIG_NFSD_V3
  1167. /*
  1168. * NFSv3 version of nfsd_create
  1169. */
  1170. __be32
  1171. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1172. char *fname, int flen, struct iattr *iap,
  1173. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1174. int *truncp, int *created)
  1175. {
  1176. struct dentry *dentry, *dchild = NULL;
  1177. struct inode *dirp;
  1178. __be32 err;
  1179. __be32 err2;
  1180. int host_err;
  1181. __u32 v_mtime=0, v_atime=0;
  1182. err = nfserr_perm;
  1183. if (!flen)
  1184. goto out;
  1185. err = nfserr_exist;
  1186. if (isdotent(fname, flen))
  1187. goto out;
  1188. if (!(iap->ia_valid & ATTR_MODE))
  1189. iap->ia_mode = 0;
  1190. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1191. if (err)
  1192. goto out;
  1193. dentry = fhp->fh_dentry;
  1194. dirp = dentry->d_inode;
  1195. /* Get all the sanity checks out of the way before
  1196. * we lock the parent. */
  1197. err = nfserr_notdir;
  1198. if (!dirp->i_op->lookup)
  1199. goto out;
  1200. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1201. /*
  1202. * Compose the response file handle.
  1203. */
  1204. dchild = lookup_one_len(fname, dentry, flen);
  1205. host_err = PTR_ERR(dchild);
  1206. if (IS_ERR(dchild))
  1207. goto out_nfserr;
  1208. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1209. if (err)
  1210. goto out;
  1211. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1212. /* solaris7 gets confused (bugid 4218508) if these have
  1213. * the high bit set, so just clear the high bits. If this is
  1214. * ever changed to use different attrs for storing the
  1215. * verifier, then do_open_lookup() will also need to be fixed
  1216. * accordingly.
  1217. */
  1218. v_mtime = verifier[0]&0x7fffffff;
  1219. v_atime = verifier[1]&0x7fffffff;
  1220. }
  1221. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1222. if (host_err)
  1223. goto out_nfserr;
  1224. if (dchild->d_inode) {
  1225. err = 0;
  1226. switch (createmode) {
  1227. case NFS3_CREATE_UNCHECKED:
  1228. if (! S_ISREG(dchild->d_inode->i_mode))
  1229. err = nfserr_exist;
  1230. else if (truncp) {
  1231. /* in nfsv4, we need to treat this case a little
  1232. * differently. we don't want to truncate the
  1233. * file now; this would be wrong if the OPEN
  1234. * fails for some other reason. furthermore,
  1235. * if the size is nonzero, we should ignore it
  1236. * according to spec!
  1237. */
  1238. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1239. }
  1240. else {
  1241. iap->ia_valid &= ATTR_SIZE;
  1242. goto set_attr;
  1243. }
  1244. break;
  1245. case NFS3_CREATE_EXCLUSIVE:
  1246. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1247. && dchild->d_inode->i_atime.tv_sec == v_atime
  1248. && dchild->d_inode->i_size == 0 )
  1249. break;
  1250. /* fallthru */
  1251. case NFS3_CREATE_GUARDED:
  1252. err = nfserr_exist;
  1253. }
  1254. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1255. goto out;
  1256. }
  1257. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1258. if (host_err < 0) {
  1259. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1260. goto out_nfserr;
  1261. }
  1262. if (created)
  1263. *created = 1;
  1264. if (EX_ISSYNC(fhp->fh_export)) {
  1265. err = nfserrno(nfsd_sync_dir(dentry));
  1266. /* setattr will sync the child (or not) */
  1267. }
  1268. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1269. /* Cram the verifier into atime/mtime */
  1270. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1271. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1272. /* XXX someone who knows this better please fix it for nsec */
  1273. iap->ia_mtime.tv_sec = v_mtime;
  1274. iap->ia_atime.tv_sec = v_atime;
  1275. iap->ia_mtime.tv_nsec = 0;
  1276. iap->ia_atime.tv_nsec = 0;
  1277. }
  1278. set_attr:
  1279. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1280. if (err2)
  1281. err = err2;
  1282. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1283. /*
  1284. * Update the filehandle to get the new inode info.
  1285. */
  1286. if (!err)
  1287. err = fh_update(resfhp);
  1288. out:
  1289. fh_unlock(fhp);
  1290. if (dchild && !IS_ERR(dchild))
  1291. dput(dchild);
  1292. return err;
  1293. out_nfserr:
  1294. err = nfserrno(host_err);
  1295. goto out;
  1296. }
  1297. #endif /* CONFIG_NFSD_V3 */
  1298. /*
  1299. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1300. * fits into the buffer. On return, it contains the true length.
  1301. * N.B. After this call fhp needs an fh_put
  1302. */
  1303. __be32
  1304. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1305. {
  1306. struct dentry *dentry;
  1307. struct inode *inode;
  1308. mm_segment_t oldfs;
  1309. __be32 err;
  1310. int host_err;
  1311. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1312. if (err)
  1313. goto out;
  1314. dentry = fhp->fh_dentry;
  1315. inode = dentry->d_inode;
  1316. err = nfserr_inval;
  1317. if (!inode->i_op->readlink)
  1318. goto out;
  1319. touch_atime(fhp->fh_export->ex_path.mnt, dentry);
  1320. /* N.B. Why does this call need a get_fs()??
  1321. * Remove the set_fs and watch the fireworks:-) --okir
  1322. */
  1323. oldfs = get_fs(); set_fs(KERNEL_DS);
  1324. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1325. set_fs(oldfs);
  1326. if (host_err < 0)
  1327. goto out_nfserr;
  1328. *lenp = host_err;
  1329. err = 0;
  1330. out:
  1331. return err;
  1332. out_nfserr:
  1333. err = nfserrno(host_err);
  1334. goto out;
  1335. }
  1336. /*
  1337. * Create a symlink and look up its inode
  1338. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1339. */
  1340. __be32
  1341. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1342. char *fname, int flen,
  1343. char *path, int plen,
  1344. struct svc_fh *resfhp,
  1345. struct iattr *iap)
  1346. {
  1347. struct dentry *dentry, *dnew;
  1348. __be32 err, cerr;
  1349. int host_err;
  1350. err = nfserr_noent;
  1351. if (!flen || !plen)
  1352. goto out;
  1353. err = nfserr_exist;
  1354. if (isdotent(fname, flen))
  1355. goto out;
  1356. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1357. if (err)
  1358. goto out;
  1359. fh_lock(fhp);
  1360. dentry = fhp->fh_dentry;
  1361. dnew = lookup_one_len(fname, dentry, flen);
  1362. host_err = PTR_ERR(dnew);
  1363. if (IS_ERR(dnew))
  1364. goto out_nfserr;
  1365. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1366. if (host_err)
  1367. goto out_nfserr;
  1368. if (unlikely(path[plen] != 0)) {
  1369. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1370. if (path_alloced == NULL)
  1371. host_err = -ENOMEM;
  1372. else {
  1373. strncpy(path_alloced, path, plen);
  1374. path_alloced[plen] = 0;
  1375. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
  1376. kfree(path_alloced);
  1377. }
  1378. } else
  1379. host_err = vfs_symlink(dentry->d_inode, dnew, path);
  1380. if (!host_err) {
  1381. if (EX_ISSYNC(fhp->fh_export))
  1382. host_err = nfsd_sync_dir(dentry);
  1383. }
  1384. err = nfserrno(host_err);
  1385. fh_unlock(fhp);
  1386. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1387. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1388. dput(dnew);
  1389. if (err==0) err = cerr;
  1390. out:
  1391. return err;
  1392. out_nfserr:
  1393. err = nfserrno(host_err);
  1394. goto out;
  1395. }
  1396. /*
  1397. * Create a hardlink
  1398. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1399. */
  1400. __be32
  1401. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1402. char *name, int len, struct svc_fh *tfhp)
  1403. {
  1404. struct dentry *ddir, *dnew, *dold;
  1405. struct inode *dirp, *dest;
  1406. __be32 err;
  1407. int host_err;
  1408. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1409. if (err)
  1410. goto out;
  1411. err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
  1412. if (err)
  1413. goto out;
  1414. err = nfserr_perm;
  1415. if (!len)
  1416. goto out;
  1417. err = nfserr_exist;
  1418. if (isdotent(name, len))
  1419. goto out;
  1420. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1421. ddir = ffhp->fh_dentry;
  1422. dirp = ddir->d_inode;
  1423. dnew = lookup_one_len(name, ddir, len);
  1424. host_err = PTR_ERR(dnew);
  1425. if (IS_ERR(dnew))
  1426. goto out_nfserr;
  1427. dold = tfhp->fh_dentry;
  1428. dest = dold->d_inode;
  1429. host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
  1430. if (host_err) {
  1431. err = nfserrno(host_err);
  1432. goto out_dput;
  1433. }
  1434. host_err = vfs_link(dold, dirp, dnew);
  1435. if (!host_err) {
  1436. if (EX_ISSYNC(ffhp->fh_export)) {
  1437. err = nfserrno(nfsd_sync_dir(ddir));
  1438. write_inode_now(dest, 1);
  1439. }
  1440. err = 0;
  1441. } else {
  1442. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1443. err = nfserr_acces;
  1444. else
  1445. err = nfserrno(host_err);
  1446. }
  1447. mnt_drop_write(tfhp->fh_export->ex_path.mnt);
  1448. out_dput:
  1449. dput(dnew);
  1450. out_unlock:
  1451. fh_unlock(ffhp);
  1452. out:
  1453. return err;
  1454. out_nfserr:
  1455. err = nfserrno(host_err);
  1456. goto out_unlock;
  1457. }
  1458. /*
  1459. * Rename a file
  1460. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1461. */
  1462. __be32
  1463. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1464. struct svc_fh *tfhp, char *tname, int tlen)
  1465. {
  1466. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1467. struct inode *fdir, *tdir;
  1468. __be32 err;
  1469. int host_err;
  1470. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1471. if (err)
  1472. goto out;
  1473. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1474. if (err)
  1475. goto out;
  1476. fdentry = ffhp->fh_dentry;
  1477. fdir = fdentry->d_inode;
  1478. tdentry = tfhp->fh_dentry;
  1479. tdir = tdentry->d_inode;
  1480. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1481. if (ffhp->fh_export != tfhp->fh_export)
  1482. goto out;
  1483. err = nfserr_perm;
  1484. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1485. goto out;
  1486. /* cannot use fh_lock as we need deadlock protective ordering
  1487. * so do it by hand */
  1488. trap = lock_rename(tdentry, fdentry);
  1489. ffhp->fh_locked = tfhp->fh_locked = 1;
  1490. fill_pre_wcc(ffhp);
  1491. fill_pre_wcc(tfhp);
  1492. odentry = lookup_one_len(fname, fdentry, flen);
  1493. host_err = PTR_ERR(odentry);
  1494. if (IS_ERR(odentry))
  1495. goto out_nfserr;
  1496. host_err = -ENOENT;
  1497. if (!odentry->d_inode)
  1498. goto out_dput_old;
  1499. host_err = -EINVAL;
  1500. if (odentry == trap)
  1501. goto out_dput_old;
  1502. ndentry = lookup_one_len(tname, tdentry, tlen);
  1503. host_err = PTR_ERR(ndentry);
  1504. if (IS_ERR(ndentry))
  1505. goto out_dput_old;
  1506. host_err = -ENOTEMPTY;
  1507. if (ndentry == trap)
  1508. goto out_dput_new;
  1509. if (svc_msnfs(ffhp) &&
  1510. ((atomic_read(&odentry->d_count) > 1)
  1511. || (atomic_read(&ndentry->d_count) > 1))) {
  1512. host_err = -EPERM;
  1513. goto out_dput_new;
  1514. }
  1515. host_err = -EXDEV;
  1516. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1517. goto out_dput_new;
  1518. host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
  1519. if (host_err)
  1520. goto out_dput_new;
  1521. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1522. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1523. host_err = nfsd_sync_dir(tdentry);
  1524. if (!host_err)
  1525. host_err = nfsd_sync_dir(fdentry);
  1526. }
  1527. mnt_drop_write(ffhp->fh_export->ex_path.mnt);
  1528. out_dput_new:
  1529. dput(ndentry);
  1530. out_dput_old:
  1531. dput(odentry);
  1532. out_nfserr:
  1533. err = nfserrno(host_err);
  1534. /* we cannot reply on fh_unlock on the two filehandles,
  1535. * as that would do the wrong thing if the two directories
  1536. * were the same, so again we do it by hand
  1537. */
  1538. fill_post_wcc(ffhp);
  1539. fill_post_wcc(tfhp);
  1540. unlock_rename(tdentry, fdentry);
  1541. ffhp->fh_locked = tfhp->fh_locked = 0;
  1542. out:
  1543. return err;
  1544. }
  1545. /*
  1546. * Unlink a file or directory
  1547. * N.B. After this call fhp needs an fh_put
  1548. */
  1549. __be32
  1550. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1551. char *fname, int flen)
  1552. {
  1553. struct dentry *dentry, *rdentry;
  1554. struct inode *dirp;
  1555. __be32 err;
  1556. int host_err;
  1557. err = nfserr_acces;
  1558. if (!flen || isdotent(fname, flen))
  1559. goto out;
  1560. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1561. if (err)
  1562. goto out;
  1563. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1564. dentry = fhp->fh_dentry;
  1565. dirp = dentry->d_inode;
  1566. rdentry = lookup_one_len(fname, dentry, flen);
  1567. host_err = PTR_ERR(rdentry);
  1568. if (IS_ERR(rdentry))
  1569. goto out_nfserr;
  1570. if (!rdentry->d_inode) {
  1571. dput(rdentry);
  1572. err = nfserr_noent;
  1573. goto out;
  1574. }
  1575. if (!type)
  1576. type = rdentry->d_inode->i_mode & S_IFMT;
  1577. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1578. if (host_err)
  1579. goto out_nfserr;
  1580. if (type != S_IFDIR) { /* It's UNLINK */
  1581. #ifdef MSNFS
  1582. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1583. (atomic_read(&rdentry->d_count) > 1)) {
  1584. host_err = -EPERM;
  1585. } else
  1586. #endif
  1587. host_err = vfs_unlink(dirp, rdentry);
  1588. } else { /* It's RMDIR */
  1589. host_err = vfs_rmdir(dirp, rdentry);
  1590. }
  1591. dput(rdentry);
  1592. if (host_err)
  1593. goto out_drop;
  1594. if (EX_ISSYNC(fhp->fh_export))
  1595. host_err = nfsd_sync_dir(dentry);
  1596. out_drop:
  1597. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1598. out_nfserr:
  1599. err = nfserrno(host_err);
  1600. out:
  1601. return err;
  1602. }
  1603. /*
  1604. * We do this buffering because we must not call back into the file
  1605. * system's ->lookup() method from the filldir callback. That may well
  1606. * deadlock a number of file systems.
  1607. *
  1608. * This is based heavily on the implementation of same in XFS.
  1609. */
  1610. struct buffered_dirent {
  1611. u64 ino;
  1612. loff_t offset;
  1613. int namlen;
  1614. unsigned int d_type;
  1615. char name[];
  1616. };
  1617. struct readdir_data {
  1618. char *dirent;
  1619. size_t used;
  1620. int full;
  1621. };
  1622. static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
  1623. loff_t offset, u64 ino, unsigned int d_type)
  1624. {
  1625. struct readdir_data *buf = __buf;
  1626. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1627. unsigned int reclen;
  1628. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1629. if (buf->used + reclen > PAGE_SIZE) {
  1630. buf->full = 1;
  1631. return -EINVAL;
  1632. }
  1633. de->namlen = namlen;
  1634. de->offset = offset;
  1635. de->ino = ino;
  1636. de->d_type = d_type;
  1637. memcpy(de->name, name, namlen);
  1638. buf->used += reclen;
  1639. return 0;
  1640. }
  1641. static int nfsd_buffered_readdir(struct file *file, filldir_t func,
  1642. struct readdir_cd *cdp, loff_t *offsetp)
  1643. {
  1644. struct readdir_data buf;
  1645. struct buffered_dirent *de;
  1646. int host_err;
  1647. int size;
  1648. loff_t offset;
  1649. buf.dirent = (void *)__get_free_page(GFP_KERNEL);
  1650. if (!buf.dirent)
  1651. return -ENOMEM;
  1652. offset = *offsetp;
  1653. while (1) {
  1654. unsigned int reclen;
  1655. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1656. buf.used = 0;
  1657. buf.full = 0;
  1658. host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
  1659. if (buf.full)
  1660. host_err = 0;
  1661. if (host_err < 0)
  1662. break;
  1663. size = buf.used;
  1664. if (!size)
  1665. break;
  1666. de = (struct buffered_dirent *)buf.dirent;
  1667. while (size > 0) {
  1668. offset = de->offset;
  1669. if (func(cdp, de->name, de->namlen, de->offset,
  1670. de->ino, de->d_type))
  1671. goto done;
  1672. if (cdp->err != nfs_ok)
  1673. goto done;
  1674. reclen = ALIGN(sizeof(*de) + de->namlen,
  1675. sizeof(u64));
  1676. size -= reclen;
  1677. de = (struct buffered_dirent *)((char *)de + reclen);
  1678. }
  1679. offset = vfs_llseek(file, 0, SEEK_CUR);
  1680. }
  1681. done:
  1682. free_page((unsigned long)(buf.dirent));
  1683. if (host_err)
  1684. return nfserrno(host_err);
  1685. *offsetp = offset;
  1686. return cdp->err;
  1687. }
  1688. /*
  1689. * Read entries from a directory.
  1690. * The NFSv3/4 verifier we ignore for now.
  1691. */
  1692. __be32
  1693. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1694. struct readdir_cd *cdp, filldir_t func)
  1695. {
  1696. __be32 err;
  1697. struct file *file;
  1698. loff_t offset = *offsetp;
  1699. err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
  1700. if (err)
  1701. goto out;
  1702. offset = vfs_llseek(file, offset, 0);
  1703. if (offset < 0) {
  1704. err = nfserrno((int)offset);
  1705. goto out_close;
  1706. }
  1707. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1708. if (err == nfserr_eof || err == nfserr_toosmall)
  1709. err = nfs_ok; /* can still be found in ->err */
  1710. out_close:
  1711. nfsd_close(file);
  1712. out:
  1713. return err;
  1714. }
  1715. /*
  1716. * Get file system stats
  1717. * N.B. After this call fhp needs an fh_put
  1718. */
  1719. __be32
  1720. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1721. {
  1722. __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1723. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1724. err = nfserr_io;
  1725. return err;
  1726. }
  1727. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1728. {
  1729. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1730. }
  1731. /*
  1732. * Check for a user's access permissions to this inode.
  1733. */
  1734. __be32
  1735. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1736. struct dentry *dentry, int acc)
  1737. {
  1738. struct inode *inode = dentry->d_inode;
  1739. int err;
  1740. if (acc == NFSD_MAY_NOP)
  1741. return 0;
  1742. #if 0
  1743. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1744. acc,
  1745. (acc & NFSD_MAY_READ)? " read" : "",
  1746. (acc & NFSD_MAY_WRITE)? " write" : "",
  1747. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1748. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1749. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1750. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1751. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1752. inode->i_mode,
  1753. IS_IMMUTABLE(inode)? " immut" : "",
  1754. IS_APPEND(inode)? " append" : "",
  1755. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1756. dprintk(" owner %d/%d user %d/%d\n",
  1757. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1758. #endif
  1759. /* Normally we reject any write/sattr etc access on a read-only file
  1760. * system. But if it is IRIX doing check on write-access for a
  1761. * device special file, we ignore rofs.
  1762. */
  1763. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1764. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1765. if (exp_rdonly(rqstp, exp) ||
  1766. __mnt_is_readonly(exp->ex_path.mnt))
  1767. return nfserr_rofs;
  1768. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1769. return nfserr_perm;
  1770. }
  1771. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1772. return nfserr_perm;
  1773. if (acc & NFSD_MAY_LOCK) {
  1774. /* If we cannot rely on authentication in NLM requests,
  1775. * just allow locks, otherwise require read permission, or
  1776. * ownership
  1777. */
  1778. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1779. return 0;
  1780. else
  1781. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1782. }
  1783. /*
  1784. * The file owner always gets access permission for accesses that
  1785. * would normally be checked at open time. This is to make
  1786. * file access work even when the client has done a fchmod(fd, 0).
  1787. *
  1788. * However, `cp foo bar' should fail nevertheless when bar is
  1789. * readonly. A sensible way to do this might be to reject all
  1790. * attempts to truncate a read-only file, because a creat() call
  1791. * always implies file truncation.
  1792. * ... but this isn't really fair. A process may reasonably call
  1793. * ftruncate on an open file descriptor on a file with perm 000.
  1794. * We must trust the client to do permission checking - using "ACCESS"
  1795. * with NFSv3.
  1796. */
  1797. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1798. inode->i_uid == current_fsuid())
  1799. return 0;
  1800. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1801. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1802. /* Allow read access to binaries even when mode 111 */
  1803. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1804. acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
  1805. err = inode_permission(inode, MAY_EXEC);
  1806. return err? nfserrno(err) : 0;
  1807. }
  1808. void
  1809. nfsd_racache_shutdown(void)
  1810. {
  1811. struct raparms *raparm, *last_raparm;
  1812. unsigned int i;
  1813. dprintk("nfsd: freeing readahead buffers.\n");
  1814. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1815. raparm = raparm_hash[i].pb_head;
  1816. while(raparm) {
  1817. last_raparm = raparm;
  1818. raparm = raparm->p_next;
  1819. kfree(last_raparm);
  1820. }
  1821. raparm_hash[i].pb_head = NULL;
  1822. }
  1823. }
  1824. /*
  1825. * Initialize readahead param cache
  1826. */
  1827. int
  1828. nfsd_racache_init(int cache_size)
  1829. {
  1830. int i;
  1831. int j = 0;
  1832. int nperbucket;
  1833. struct raparms **raparm = NULL;
  1834. if (raparm_hash[0].pb_head)
  1835. return 0;
  1836. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1837. if (nperbucket < 2)
  1838. nperbucket = 2;
  1839. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1840. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1841. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1842. spin_lock_init(&raparm_hash[i].pb_lock);
  1843. raparm = &raparm_hash[i].pb_head;
  1844. for (j = 0; j < nperbucket; j++) {
  1845. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1846. if (!*raparm)
  1847. goto out_nomem;
  1848. raparm = &(*raparm)->p_next;
  1849. }
  1850. *raparm = NULL;
  1851. }
  1852. nfsdstats.ra_size = cache_size;
  1853. return 0;
  1854. out_nomem:
  1855. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1856. nfsd_racache_shutdown();
  1857. return -ENOMEM;
  1858. }
  1859. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1860. struct posix_acl *
  1861. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1862. {
  1863. struct inode *inode = fhp->fh_dentry->d_inode;
  1864. char *name;
  1865. void *value = NULL;
  1866. ssize_t size;
  1867. struct posix_acl *acl;
  1868. if (!IS_POSIXACL(inode))
  1869. return ERR_PTR(-EOPNOTSUPP);
  1870. switch (type) {
  1871. case ACL_TYPE_ACCESS:
  1872. name = POSIX_ACL_XATTR_ACCESS;
  1873. break;
  1874. case ACL_TYPE_DEFAULT:
  1875. name = POSIX_ACL_XATTR_DEFAULT;
  1876. break;
  1877. default:
  1878. return ERR_PTR(-EOPNOTSUPP);
  1879. }
  1880. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1881. if (size < 0)
  1882. return ERR_PTR(size);
  1883. acl = posix_acl_from_xattr(value, size);
  1884. kfree(value);
  1885. return acl;
  1886. }
  1887. int
  1888. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1889. {
  1890. struct inode *inode = fhp->fh_dentry->d_inode;
  1891. char *name;
  1892. void *value = NULL;
  1893. size_t size;
  1894. int error;
  1895. if (!IS_POSIXACL(inode) ||
  1896. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1897. return -EOPNOTSUPP;
  1898. switch(type) {
  1899. case ACL_TYPE_ACCESS:
  1900. name = POSIX_ACL_XATTR_ACCESS;
  1901. break;
  1902. case ACL_TYPE_DEFAULT:
  1903. name = POSIX_ACL_XATTR_DEFAULT;
  1904. break;
  1905. default:
  1906. return -EOPNOTSUPP;
  1907. }
  1908. if (acl && acl->a_count) {
  1909. size = posix_acl_xattr_size(acl->a_count);
  1910. value = kmalloc(size, GFP_KERNEL);
  1911. if (!value)
  1912. return -ENOMEM;
  1913. error = posix_acl_to_xattr(acl, value, size);
  1914. if (error < 0)
  1915. goto getout;
  1916. size = error;
  1917. } else
  1918. size = 0;
  1919. error = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1920. if (error)
  1921. goto getout;
  1922. if (size)
  1923. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1924. else {
  1925. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1926. error = 0;
  1927. else {
  1928. error = vfs_removexattr(fhp->fh_dentry, name);
  1929. if (error == -ENODATA)
  1930. error = 0;
  1931. }
  1932. }
  1933. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1934. getout:
  1935. kfree(value);
  1936. return error;
  1937. }
  1938. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */