vfs.c 53 KB

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