vfs.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  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. static __be32
  851. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  852. loff_t offset, struct kvec *vec, int vlen,
  853. unsigned long *cnt, int *stablep)
  854. {
  855. struct svc_export *exp;
  856. struct dentry *dentry;
  857. struct inode *inode;
  858. mm_segment_t oldfs;
  859. __be32 err = 0;
  860. int host_err;
  861. int stable = *stablep;
  862. #ifdef MSNFS
  863. err = nfserr_perm;
  864. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  865. (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
  866. goto out;
  867. #endif
  868. dentry = file->f_path.dentry;
  869. inode = dentry->d_inode;
  870. exp = fhp->fh_export;
  871. /*
  872. * Request sync writes if
  873. * - the sync export option has been set, or
  874. * - the client requested O_SYNC behavior (NFSv3 feature).
  875. * - The file system doesn't support fsync().
  876. * When gathered writes have been configured for this volume,
  877. * flushing the data to disk is handled separately below.
  878. */
  879. if (!file->f_op->fsync) {/* COMMIT3 cannot work */
  880. stable = 2;
  881. *stablep = 2; /* FILE_SYNC */
  882. }
  883. if (!EX_ISSYNC(exp))
  884. stable = 0;
  885. if (stable && !EX_WGATHER(exp)) {
  886. spin_lock(&file->f_lock);
  887. file->f_flags |= O_SYNC;
  888. spin_unlock(&file->f_lock);
  889. }
  890. /* Write the data. */
  891. oldfs = get_fs(); set_fs(KERNEL_DS);
  892. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  893. set_fs(oldfs);
  894. if (host_err >= 0) {
  895. nfsdstats.io_write += host_err;
  896. fsnotify_modify(file->f_path.dentry);
  897. }
  898. /* clear setuid/setgid flag after write */
  899. if (host_err >= 0 && (inode->i_mode & (S_ISUID | S_ISGID)))
  900. kill_suid(dentry);
  901. if (host_err >= 0 && stable) {
  902. static ino_t last_ino;
  903. static dev_t last_dev;
  904. /*
  905. * Gathered writes: If another process is currently
  906. * writing to the file, there's a high chance
  907. * this is another nfsd (triggered by a bulk write
  908. * from a client's biod). Rather than syncing the
  909. * file with each write request, we sleep for 10 msec.
  910. *
  911. * I don't know if this roughly approximates
  912. * C. Juszak's idea of gathered writes, but it's a
  913. * nice and simple solution (IMHO), and it seems to
  914. * work:-)
  915. */
  916. if (EX_WGATHER(exp)) {
  917. if (atomic_read(&inode->i_writecount) > 1
  918. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  919. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  920. msleep(10);
  921. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  922. }
  923. if (inode->i_state & I_DIRTY) {
  924. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  925. host_err=nfsd_sync(file);
  926. }
  927. #if 0
  928. wake_up(&inode->i_wait);
  929. #endif
  930. }
  931. last_ino = inode->i_ino;
  932. last_dev = inode->i_sb->s_dev;
  933. }
  934. dprintk("nfsd: write complete host_err=%d\n", host_err);
  935. if (host_err >= 0) {
  936. err = 0;
  937. *cnt = host_err;
  938. } else
  939. err = nfserrno(host_err);
  940. out:
  941. return err;
  942. }
  943. /*
  944. * Read data from a file. count must contain the requested read count
  945. * on entry. On return, *count contains the number of bytes actually read.
  946. * N.B. After this call fhp needs an fh_put
  947. */
  948. __be32
  949. nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  950. loff_t offset, struct kvec *vec, int vlen,
  951. unsigned long *count)
  952. {
  953. __be32 err;
  954. if (file) {
  955. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  956. NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
  957. if (err)
  958. goto out;
  959. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  960. } else {
  961. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  962. if (err)
  963. goto out;
  964. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  965. nfsd_close(file);
  966. }
  967. out:
  968. return err;
  969. }
  970. /*
  971. * Write data to a file.
  972. * The stable flag requests synchronous writes.
  973. * N.B. After this call fhp needs an fh_put
  974. */
  975. __be32
  976. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  977. loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
  978. int *stablep)
  979. {
  980. __be32 err = 0;
  981. if (file) {
  982. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  983. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  984. if (err)
  985. goto out;
  986. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  987. stablep);
  988. } else {
  989. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  990. if (err)
  991. goto out;
  992. if (cnt)
  993. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  994. cnt, stablep);
  995. nfsd_close(file);
  996. }
  997. out:
  998. return err;
  999. }
  1000. #ifdef CONFIG_NFSD_V3
  1001. /*
  1002. * Commit all pending writes to stable storage.
  1003. * Strictly speaking, we could sync just the indicated file region here,
  1004. * but there's currently no way we can ask the VFS to do so.
  1005. *
  1006. * Unfortunately we cannot lock the file to make sure we return full WCC
  1007. * data to the client, as locking happens lower down in the filesystem.
  1008. */
  1009. __be32
  1010. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1011. loff_t offset, unsigned long count)
  1012. {
  1013. struct file *file;
  1014. __be32 err;
  1015. if ((u64)count > ~(u64)offset)
  1016. return nfserr_inval;
  1017. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  1018. if (err)
  1019. return err;
  1020. if (EX_ISSYNC(fhp->fh_export)) {
  1021. if (file->f_op && file->f_op->fsync) {
  1022. err = nfserrno(nfsd_sync(file));
  1023. } else {
  1024. err = nfserr_notsupp;
  1025. }
  1026. }
  1027. nfsd_close(file);
  1028. return err;
  1029. }
  1030. #endif /* CONFIG_NFSD_V3 */
  1031. static __be32
  1032. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1033. struct iattr *iap)
  1034. {
  1035. /*
  1036. * Mode has already been set earlier in create:
  1037. */
  1038. iap->ia_valid &= ~ATTR_MODE;
  1039. /*
  1040. * Setting uid/gid works only for root. Irix appears to
  1041. * send along the gid on create when it tries to implement
  1042. * setgid directories via NFS:
  1043. */
  1044. if (current_fsuid() != 0)
  1045. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1046. if (iap->ia_valid)
  1047. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1048. return 0;
  1049. }
  1050. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1051. * setting size to 0 may fail for some specific file systems by the permission
  1052. * checking which requires WRITE permission but the mode is 000.
  1053. * we ignore the resizing(to 0) on the just new created file, since the size is
  1054. * 0 after file created.
  1055. *
  1056. * call this only after vfs_create() is called.
  1057. * */
  1058. static void
  1059. nfsd_check_ignore_resizing(struct iattr *iap)
  1060. {
  1061. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1062. iap->ia_valid &= ~ATTR_SIZE;
  1063. }
  1064. /*
  1065. * Create a file (regular, directory, device, fifo); UNIX sockets
  1066. * not yet implemented.
  1067. * If the response fh has been verified, the parent directory should
  1068. * already be locked. Note that the parent directory is left locked.
  1069. *
  1070. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1071. */
  1072. __be32
  1073. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1074. char *fname, int flen, struct iattr *iap,
  1075. int type, dev_t rdev, struct svc_fh *resfhp)
  1076. {
  1077. struct dentry *dentry, *dchild = NULL;
  1078. struct inode *dirp;
  1079. __be32 err;
  1080. __be32 err2;
  1081. int host_err;
  1082. err = nfserr_perm;
  1083. if (!flen)
  1084. goto out;
  1085. err = nfserr_exist;
  1086. if (isdotent(fname, flen))
  1087. goto out;
  1088. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1089. if (err)
  1090. goto out;
  1091. dentry = fhp->fh_dentry;
  1092. dirp = dentry->d_inode;
  1093. err = nfserr_notdir;
  1094. if (!dirp->i_op->lookup)
  1095. goto out;
  1096. /*
  1097. * Check whether the response file handle has been verified yet.
  1098. * If it has, the parent directory should already be locked.
  1099. */
  1100. if (!resfhp->fh_dentry) {
  1101. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1102. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1103. dchild = lookup_one_len(fname, dentry, flen);
  1104. host_err = PTR_ERR(dchild);
  1105. if (IS_ERR(dchild))
  1106. goto out_nfserr;
  1107. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1108. if (err)
  1109. goto out;
  1110. } else {
  1111. /* called from nfsd_proc_create */
  1112. dchild = dget(resfhp->fh_dentry);
  1113. if (!fhp->fh_locked) {
  1114. /* not actually possible */
  1115. printk(KERN_ERR
  1116. "nfsd_create: parent %s/%s not locked!\n",
  1117. dentry->d_parent->d_name.name,
  1118. dentry->d_name.name);
  1119. err = nfserr_io;
  1120. goto out;
  1121. }
  1122. }
  1123. /*
  1124. * Make sure the child dentry is still negative ...
  1125. */
  1126. err = nfserr_exist;
  1127. if (dchild->d_inode) {
  1128. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1129. dentry->d_name.name, dchild->d_name.name);
  1130. goto out;
  1131. }
  1132. if (!(iap->ia_valid & ATTR_MODE))
  1133. iap->ia_mode = 0;
  1134. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1135. err = nfserr_inval;
  1136. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1137. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1138. type);
  1139. goto out;
  1140. }
  1141. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1142. if (host_err)
  1143. goto out_nfserr;
  1144. /*
  1145. * Get the dir op function pointer.
  1146. */
  1147. err = 0;
  1148. switch (type) {
  1149. case S_IFREG:
  1150. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1151. if (!host_err)
  1152. nfsd_check_ignore_resizing(iap);
  1153. break;
  1154. case S_IFDIR:
  1155. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1156. break;
  1157. case S_IFCHR:
  1158. case S_IFBLK:
  1159. case S_IFIFO:
  1160. case S_IFSOCK:
  1161. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1162. break;
  1163. }
  1164. if (host_err < 0) {
  1165. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1166. goto out_nfserr;
  1167. }
  1168. if (EX_ISSYNC(fhp->fh_export)) {
  1169. err = nfserrno(nfsd_sync_dir(dentry));
  1170. write_inode_now(dchild->d_inode, 1);
  1171. }
  1172. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1173. if (err2)
  1174. err = err2;
  1175. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1176. /*
  1177. * Update the file handle to get the new inode info.
  1178. */
  1179. if (!err)
  1180. err = fh_update(resfhp);
  1181. out:
  1182. if (dchild && !IS_ERR(dchild))
  1183. dput(dchild);
  1184. return err;
  1185. out_nfserr:
  1186. err = nfserrno(host_err);
  1187. goto out;
  1188. }
  1189. #ifdef CONFIG_NFSD_V3
  1190. /*
  1191. * NFSv3 version of nfsd_create
  1192. */
  1193. __be32
  1194. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1195. char *fname, int flen, struct iattr *iap,
  1196. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1197. int *truncp, int *created)
  1198. {
  1199. struct dentry *dentry, *dchild = NULL;
  1200. struct inode *dirp;
  1201. __be32 err;
  1202. __be32 err2;
  1203. int host_err;
  1204. __u32 v_mtime=0, v_atime=0;
  1205. err = nfserr_perm;
  1206. if (!flen)
  1207. goto out;
  1208. err = nfserr_exist;
  1209. if (isdotent(fname, flen))
  1210. goto out;
  1211. if (!(iap->ia_valid & ATTR_MODE))
  1212. iap->ia_mode = 0;
  1213. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1214. if (err)
  1215. goto out;
  1216. dentry = fhp->fh_dentry;
  1217. dirp = dentry->d_inode;
  1218. /* Get all the sanity checks out of the way before
  1219. * we lock the parent. */
  1220. err = nfserr_notdir;
  1221. if (!dirp->i_op->lookup)
  1222. goto out;
  1223. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1224. /*
  1225. * Compose the response file handle.
  1226. */
  1227. dchild = lookup_one_len(fname, dentry, flen);
  1228. host_err = PTR_ERR(dchild);
  1229. if (IS_ERR(dchild))
  1230. goto out_nfserr;
  1231. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1232. if (err)
  1233. goto out;
  1234. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1235. /* solaris7 gets confused (bugid 4218508) if these have
  1236. * the high bit set, so just clear the high bits. If this is
  1237. * ever changed to use different attrs for storing the
  1238. * verifier, then do_open_lookup() will also need to be fixed
  1239. * accordingly.
  1240. */
  1241. v_mtime = verifier[0]&0x7fffffff;
  1242. v_atime = verifier[1]&0x7fffffff;
  1243. }
  1244. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1245. if (host_err)
  1246. goto out_nfserr;
  1247. if (dchild->d_inode) {
  1248. err = 0;
  1249. switch (createmode) {
  1250. case NFS3_CREATE_UNCHECKED:
  1251. if (! S_ISREG(dchild->d_inode->i_mode))
  1252. err = nfserr_exist;
  1253. else if (truncp) {
  1254. /* in nfsv4, we need to treat this case a little
  1255. * differently. we don't want to truncate the
  1256. * file now; this would be wrong if the OPEN
  1257. * fails for some other reason. furthermore,
  1258. * if the size is nonzero, we should ignore it
  1259. * according to spec!
  1260. */
  1261. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1262. }
  1263. else {
  1264. iap->ia_valid &= ATTR_SIZE;
  1265. goto set_attr;
  1266. }
  1267. break;
  1268. case NFS3_CREATE_EXCLUSIVE:
  1269. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1270. && dchild->d_inode->i_atime.tv_sec == v_atime
  1271. && dchild->d_inode->i_size == 0 )
  1272. break;
  1273. /* fallthru */
  1274. case NFS3_CREATE_GUARDED:
  1275. err = nfserr_exist;
  1276. }
  1277. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1278. goto out;
  1279. }
  1280. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1281. if (host_err < 0) {
  1282. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1283. goto out_nfserr;
  1284. }
  1285. if (created)
  1286. *created = 1;
  1287. if (EX_ISSYNC(fhp->fh_export)) {
  1288. err = nfserrno(nfsd_sync_dir(dentry));
  1289. /* setattr will sync the child (or not) */
  1290. }
  1291. nfsd_check_ignore_resizing(iap);
  1292. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1293. /* Cram the verifier into atime/mtime */
  1294. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1295. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1296. /* XXX someone who knows this better please fix it for nsec */
  1297. iap->ia_mtime.tv_sec = v_mtime;
  1298. iap->ia_atime.tv_sec = v_atime;
  1299. iap->ia_mtime.tv_nsec = 0;
  1300. iap->ia_atime.tv_nsec = 0;
  1301. }
  1302. set_attr:
  1303. err2 = nfsd_create_setattr(rqstp, resfhp, iap);
  1304. if (err2)
  1305. err = err2;
  1306. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1307. /*
  1308. * Update the filehandle to get the new inode info.
  1309. */
  1310. if (!err)
  1311. err = fh_update(resfhp);
  1312. out:
  1313. fh_unlock(fhp);
  1314. if (dchild && !IS_ERR(dchild))
  1315. dput(dchild);
  1316. return err;
  1317. out_nfserr:
  1318. err = nfserrno(host_err);
  1319. goto out;
  1320. }
  1321. #endif /* CONFIG_NFSD_V3 */
  1322. /*
  1323. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1324. * fits into the buffer. On return, it contains the true length.
  1325. * N.B. After this call fhp needs an fh_put
  1326. */
  1327. __be32
  1328. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1329. {
  1330. struct dentry *dentry;
  1331. struct inode *inode;
  1332. mm_segment_t oldfs;
  1333. __be32 err;
  1334. int host_err;
  1335. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1336. if (err)
  1337. goto out;
  1338. dentry = fhp->fh_dentry;
  1339. inode = dentry->d_inode;
  1340. err = nfserr_inval;
  1341. if (!inode->i_op->readlink)
  1342. goto out;
  1343. touch_atime(fhp->fh_export->ex_path.mnt, dentry);
  1344. /* N.B. Why does this call need a get_fs()??
  1345. * Remove the set_fs and watch the fireworks:-) --okir
  1346. */
  1347. oldfs = get_fs(); set_fs(KERNEL_DS);
  1348. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1349. set_fs(oldfs);
  1350. if (host_err < 0)
  1351. goto out_nfserr;
  1352. *lenp = host_err;
  1353. err = 0;
  1354. out:
  1355. return err;
  1356. out_nfserr:
  1357. err = nfserrno(host_err);
  1358. goto out;
  1359. }
  1360. /*
  1361. * Create a symlink and look up its inode
  1362. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1363. */
  1364. __be32
  1365. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1366. char *fname, int flen,
  1367. char *path, int plen,
  1368. struct svc_fh *resfhp,
  1369. struct iattr *iap)
  1370. {
  1371. struct dentry *dentry, *dnew;
  1372. __be32 err, cerr;
  1373. int host_err;
  1374. err = nfserr_noent;
  1375. if (!flen || !plen)
  1376. goto out;
  1377. err = nfserr_exist;
  1378. if (isdotent(fname, flen))
  1379. goto out;
  1380. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1381. if (err)
  1382. goto out;
  1383. fh_lock(fhp);
  1384. dentry = fhp->fh_dentry;
  1385. dnew = lookup_one_len(fname, dentry, flen);
  1386. host_err = PTR_ERR(dnew);
  1387. if (IS_ERR(dnew))
  1388. goto out_nfserr;
  1389. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1390. if (host_err)
  1391. goto out_nfserr;
  1392. if (unlikely(path[plen] != 0)) {
  1393. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1394. if (path_alloced == NULL)
  1395. host_err = -ENOMEM;
  1396. else {
  1397. strncpy(path_alloced, path, plen);
  1398. path_alloced[plen] = 0;
  1399. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
  1400. kfree(path_alloced);
  1401. }
  1402. } else
  1403. host_err = vfs_symlink(dentry->d_inode, dnew, path);
  1404. if (!host_err) {
  1405. if (EX_ISSYNC(fhp->fh_export))
  1406. host_err = nfsd_sync_dir(dentry);
  1407. }
  1408. err = nfserrno(host_err);
  1409. fh_unlock(fhp);
  1410. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1411. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1412. dput(dnew);
  1413. if (err==0) err = cerr;
  1414. out:
  1415. return err;
  1416. out_nfserr:
  1417. err = nfserrno(host_err);
  1418. goto out;
  1419. }
  1420. /*
  1421. * Create a hardlink
  1422. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1423. */
  1424. __be32
  1425. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1426. char *name, int len, struct svc_fh *tfhp)
  1427. {
  1428. struct dentry *ddir, *dnew, *dold;
  1429. struct inode *dirp, *dest;
  1430. __be32 err;
  1431. int host_err;
  1432. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1433. if (err)
  1434. goto out;
  1435. err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
  1436. if (err)
  1437. goto out;
  1438. err = nfserr_perm;
  1439. if (!len)
  1440. goto out;
  1441. err = nfserr_exist;
  1442. if (isdotent(name, len))
  1443. goto out;
  1444. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1445. ddir = ffhp->fh_dentry;
  1446. dirp = ddir->d_inode;
  1447. dnew = lookup_one_len(name, ddir, len);
  1448. host_err = PTR_ERR(dnew);
  1449. if (IS_ERR(dnew))
  1450. goto out_nfserr;
  1451. dold = tfhp->fh_dentry;
  1452. dest = dold->d_inode;
  1453. host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
  1454. if (host_err) {
  1455. err = nfserrno(host_err);
  1456. goto out_dput;
  1457. }
  1458. host_err = vfs_link(dold, dirp, dnew);
  1459. if (!host_err) {
  1460. if (EX_ISSYNC(ffhp->fh_export)) {
  1461. err = nfserrno(nfsd_sync_dir(ddir));
  1462. write_inode_now(dest, 1);
  1463. }
  1464. err = 0;
  1465. } else {
  1466. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1467. err = nfserr_acces;
  1468. else
  1469. err = nfserrno(host_err);
  1470. }
  1471. mnt_drop_write(tfhp->fh_export->ex_path.mnt);
  1472. out_dput:
  1473. dput(dnew);
  1474. out_unlock:
  1475. fh_unlock(ffhp);
  1476. out:
  1477. return err;
  1478. out_nfserr:
  1479. err = nfserrno(host_err);
  1480. goto out_unlock;
  1481. }
  1482. /*
  1483. * Rename a file
  1484. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1485. */
  1486. __be32
  1487. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1488. struct svc_fh *tfhp, char *tname, int tlen)
  1489. {
  1490. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1491. struct inode *fdir, *tdir;
  1492. __be32 err;
  1493. int host_err;
  1494. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1495. if (err)
  1496. goto out;
  1497. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1498. if (err)
  1499. goto out;
  1500. fdentry = ffhp->fh_dentry;
  1501. fdir = fdentry->d_inode;
  1502. tdentry = tfhp->fh_dentry;
  1503. tdir = tdentry->d_inode;
  1504. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1505. if (ffhp->fh_export != tfhp->fh_export)
  1506. goto out;
  1507. err = nfserr_perm;
  1508. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1509. goto out;
  1510. /* cannot use fh_lock as we need deadlock protective ordering
  1511. * so do it by hand */
  1512. trap = lock_rename(tdentry, fdentry);
  1513. ffhp->fh_locked = tfhp->fh_locked = 1;
  1514. fill_pre_wcc(ffhp);
  1515. fill_pre_wcc(tfhp);
  1516. odentry = lookup_one_len(fname, fdentry, flen);
  1517. host_err = PTR_ERR(odentry);
  1518. if (IS_ERR(odentry))
  1519. goto out_nfserr;
  1520. host_err = -ENOENT;
  1521. if (!odentry->d_inode)
  1522. goto out_dput_old;
  1523. host_err = -EINVAL;
  1524. if (odentry == trap)
  1525. goto out_dput_old;
  1526. ndentry = lookup_one_len(tname, tdentry, tlen);
  1527. host_err = PTR_ERR(ndentry);
  1528. if (IS_ERR(ndentry))
  1529. goto out_dput_old;
  1530. host_err = -ENOTEMPTY;
  1531. if (ndentry == trap)
  1532. goto out_dput_new;
  1533. if (svc_msnfs(ffhp) &&
  1534. ((atomic_read(&odentry->d_count) > 1)
  1535. || (atomic_read(&ndentry->d_count) > 1))) {
  1536. host_err = -EPERM;
  1537. goto out_dput_new;
  1538. }
  1539. host_err = -EXDEV;
  1540. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1541. goto out_dput_new;
  1542. host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
  1543. if (host_err)
  1544. goto out_dput_new;
  1545. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1546. if (!host_err && EX_ISSYNC(tfhp->fh_export)) {
  1547. host_err = nfsd_sync_dir(tdentry);
  1548. if (!host_err)
  1549. host_err = nfsd_sync_dir(fdentry);
  1550. }
  1551. mnt_drop_write(ffhp->fh_export->ex_path.mnt);
  1552. out_dput_new:
  1553. dput(ndentry);
  1554. out_dput_old:
  1555. dput(odentry);
  1556. out_nfserr:
  1557. err = nfserrno(host_err);
  1558. /* we cannot reply on fh_unlock on the two filehandles,
  1559. * as that would do the wrong thing if the two directories
  1560. * were the same, so again we do it by hand
  1561. */
  1562. fill_post_wcc(ffhp);
  1563. fill_post_wcc(tfhp);
  1564. unlock_rename(tdentry, fdentry);
  1565. ffhp->fh_locked = tfhp->fh_locked = 0;
  1566. out:
  1567. return err;
  1568. }
  1569. /*
  1570. * Unlink a file or directory
  1571. * N.B. After this call fhp needs an fh_put
  1572. */
  1573. __be32
  1574. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1575. char *fname, int flen)
  1576. {
  1577. struct dentry *dentry, *rdentry;
  1578. struct inode *dirp;
  1579. __be32 err;
  1580. int host_err;
  1581. err = nfserr_acces;
  1582. if (!flen || isdotent(fname, flen))
  1583. goto out;
  1584. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1585. if (err)
  1586. goto out;
  1587. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1588. dentry = fhp->fh_dentry;
  1589. dirp = dentry->d_inode;
  1590. rdentry = lookup_one_len(fname, dentry, flen);
  1591. host_err = PTR_ERR(rdentry);
  1592. if (IS_ERR(rdentry))
  1593. goto out_nfserr;
  1594. if (!rdentry->d_inode) {
  1595. dput(rdentry);
  1596. err = nfserr_noent;
  1597. goto out;
  1598. }
  1599. if (!type)
  1600. type = rdentry->d_inode->i_mode & S_IFMT;
  1601. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1602. if (host_err)
  1603. goto out_nfserr;
  1604. if (type != S_IFDIR) { /* It's UNLINK */
  1605. #ifdef MSNFS
  1606. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  1607. (atomic_read(&rdentry->d_count) > 1)) {
  1608. host_err = -EPERM;
  1609. } else
  1610. #endif
  1611. host_err = vfs_unlink(dirp, rdentry);
  1612. } else { /* It's RMDIR */
  1613. host_err = vfs_rmdir(dirp, rdentry);
  1614. }
  1615. dput(rdentry);
  1616. if (host_err)
  1617. goto out_drop;
  1618. if (EX_ISSYNC(fhp->fh_export))
  1619. host_err = nfsd_sync_dir(dentry);
  1620. out_drop:
  1621. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1622. out_nfserr:
  1623. err = nfserrno(host_err);
  1624. out:
  1625. return err;
  1626. }
  1627. /*
  1628. * We do this buffering because we must not call back into the file
  1629. * system's ->lookup() method from the filldir callback. That may well
  1630. * deadlock a number of file systems.
  1631. *
  1632. * This is based heavily on the implementation of same in XFS.
  1633. */
  1634. struct buffered_dirent {
  1635. u64 ino;
  1636. loff_t offset;
  1637. int namlen;
  1638. unsigned int d_type;
  1639. char name[];
  1640. };
  1641. struct readdir_data {
  1642. char *dirent;
  1643. size_t used;
  1644. int full;
  1645. };
  1646. static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
  1647. loff_t offset, u64 ino, unsigned int d_type)
  1648. {
  1649. struct readdir_data *buf = __buf;
  1650. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1651. unsigned int reclen;
  1652. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1653. if (buf->used + reclen > PAGE_SIZE) {
  1654. buf->full = 1;
  1655. return -EINVAL;
  1656. }
  1657. de->namlen = namlen;
  1658. de->offset = offset;
  1659. de->ino = ino;
  1660. de->d_type = d_type;
  1661. memcpy(de->name, name, namlen);
  1662. buf->used += reclen;
  1663. return 0;
  1664. }
  1665. static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
  1666. struct readdir_cd *cdp, loff_t *offsetp)
  1667. {
  1668. struct readdir_data buf;
  1669. struct buffered_dirent *de;
  1670. int host_err;
  1671. int size;
  1672. loff_t offset;
  1673. buf.dirent = (void *)__get_free_page(GFP_KERNEL);
  1674. if (!buf.dirent)
  1675. return nfserrno(-ENOMEM);
  1676. offset = *offsetp;
  1677. while (1) {
  1678. struct inode *dir_inode = file->f_path.dentry->d_inode;
  1679. unsigned int reclen;
  1680. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1681. buf.used = 0;
  1682. buf.full = 0;
  1683. host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
  1684. if (buf.full)
  1685. host_err = 0;
  1686. if (host_err < 0)
  1687. break;
  1688. size = buf.used;
  1689. if (!size)
  1690. break;
  1691. /*
  1692. * Various filldir functions may end up calling back into
  1693. * lookup_one_len() and the file system's ->lookup() method.
  1694. * These expect i_mutex to be held, as it would within readdir.
  1695. */
  1696. host_err = mutex_lock_killable(&dir_inode->i_mutex);
  1697. if (host_err)
  1698. break;
  1699. de = (struct buffered_dirent *)buf.dirent;
  1700. while (size > 0) {
  1701. offset = de->offset;
  1702. if (func(cdp, de->name, de->namlen, de->offset,
  1703. de->ino, de->d_type))
  1704. break;
  1705. if (cdp->err != nfs_ok)
  1706. break;
  1707. reclen = ALIGN(sizeof(*de) + de->namlen,
  1708. sizeof(u64));
  1709. size -= reclen;
  1710. de = (struct buffered_dirent *)((char *)de + reclen);
  1711. }
  1712. mutex_unlock(&dir_inode->i_mutex);
  1713. if (size > 0) /* We bailed out early */
  1714. break;
  1715. offset = vfs_llseek(file, 0, SEEK_CUR);
  1716. }
  1717. free_page((unsigned long)(buf.dirent));
  1718. if (host_err)
  1719. return nfserrno(host_err);
  1720. *offsetp = offset;
  1721. return cdp->err;
  1722. }
  1723. /*
  1724. * Read entries from a directory.
  1725. * The NFSv3/4 verifier we ignore for now.
  1726. */
  1727. __be32
  1728. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1729. struct readdir_cd *cdp, filldir_t func)
  1730. {
  1731. __be32 err;
  1732. struct file *file;
  1733. loff_t offset = *offsetp;
  1734. err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
  1735. if (err)
  1736. goto out;
  1737. offset = vfs_llseek(file, offset, 0);
  1738. if (offset < 0) {
  1739. err = nfserrno((int)offset);
  1740. goto out_close;
  1741. }
  1742. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1743. if (err == nfserr_eof || err == nfserr_toosmall)
  1744. err = nfs_ok; /* can still be found in ->err */
  1745. out_close:
  1746. nfsd_close(file);
  1747. out:
  1748. return err;
  1749. }
  1750. /*
  1751. * Get file system stats
  1752. * N.B. After this call fhp needs an fh_put
  1753. */
  1754. __be32
  1755. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1756. {
  1757. __be32 err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1758. if (!err && vfs_statfs(fhp->fh_dentry,stat))
  1759. err = nfserr_io;
  1760. return err;
  1761. }
  1762. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1763. {
  1764. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1765. }
  1766. /*
  1767. * Check for a user's access permissions to this inode.
  1768. */
  1769. __be32
  1770. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1771. struct dentry *dentry, int acc)
  1772. {
  1773. struct inode *inode = dentry->d_inode;
  1774. int err;
  1775. if (acc == NFSD_MAY_NOP)
  1776. return 0;
  1777. #if 0
  1778. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1779. acc,
  1780. (acc & NFSD_MAY_READ)? " read" : "",
  1781. (acc & NFSD_MAY_WRITE)? " write" : "",
  1782. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1783. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1784. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1785. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1786. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1787. inode->i_mode,
  1788. IS_IMMUTABLE(inode)? " immut" : "",
  1789. IS_APPEND(inode)? " append" : "",
  1790. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1791. dprintk(" owner %d/%d user %d/%d\n",
  1792. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1793. #endif
  1794. /* Normally we reject any write/sattr etc access on a read-only file
  1795. * system. But if it is IRIX doing check on write-access for a
  1796. * device special file, we ignore rofs.
  1797. */
  1798. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1799. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1800. if (exp_rdonly(rqstp, exp) ||
  1801. __mnt_is_readonly(exp->ex_path.mnt))
  1802. return nfserr_rofs;
  1803. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1804. return nfserr_perm;
  1805. }
  1806. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1807. return nfserr_perm;
  1808. if (acc & NFSD_MAY_LOCK) {
  1809. /* If we cannot rely on authentication in NLM requests,
  1810. * just allow locks, otherwise require read permission, or
  1811. * ownership
  1812. */
  1813. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1814. return 0;
  1815. else
  1816. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1817. }
  1818. /*
  1819. * The file owner always gets access permission for accesses that
  1820. * would normally be checked at open time. This is to make
  1821. * file access work even when the client has done a fchmod(fd, 0).
  1822. *
  1823. * However, `cp foo bar' should fail nevertheless when bar is
  1824. * readonly. A sensible way to do this might be to reject all
  1825. * attempts to truncate a read-only file, because a creat() call
  1826. * always implies file truncation.
  1827. * ... but this isn't really fair. A process may reasonably call
  1828. * ftruncate on an open file descriptor on a file with perm 000.
  1829. * We must trust the client to do permission checking - using "ACCESS"
  1830. * with NFSv3.
  1831. */
  1832. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1833. inode->i_uid == current_fsuid())
  1834. return 0;
  1835. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1836. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1837. /* Allow read access to binaries even when mode 111 */
  1838. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1839. acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
  1840. err = inode_permission(inode, MAY_EXEC);
  1841. return err? nfserrno(err) : 0;
  1842. }
  1843. void
  1844. nfsd_racache_shutdown(void)
  1845. {
  1846. struct raparms *raparm, *last_raparm;
  1847. unsigned int i;
  1848. dprintk("nfsd: freeing readahead buffers.\n");
  1849. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1850. raparm = raparm_hash[i].pb_head;
  1851. while(raparm) {
  1852. last_raparm = raparm;
  1853. raparm = raparm->p_next;
  1854. kfree(last_raparm);
  1855. }
  1856. raparm_hash[i].pb_head = NULL;
  1857. }
  1858. }
  1859. /*
  1860. * Initialize readahead param cache
  1861. */
  1862. int
  1863. nfsd_racache_init(int cache_size)
  1864. {
  1865. int i;
  1866. int j = 0;
  1867. int nperbucket;
  1868. struct raparms **raparm = NULL;
  1869. if (raparm_hash[0].pb_head)
  1870. return 0;
  1871. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1872. if (nperbucket < 2)
  1873. nperbucket = 2;
  1874. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1875. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1876. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1877. spin_lock_init(&raparm_hash[i].pb_lock);
  1878. raparm = &raparm_hash[i].pb_head;
  1879. for (j = 0; j < nperbucket; j++) {
  1880. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1881. if (!*raparm)
  1882. goto out_nomem;
  1883. raparm = &(*raparm)->p_next;
  1884. }
  1885. *raparm = NULL;
  1886. }
  1887. nfsdstats.ra_size = cache_size;
  1888. return 0;
  1889. out_nomem:
  1890. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1891. nfsd_racache_shutdown();
  1892. return -ENOMEM;
  1893. }
  1894. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1895. struct posix_acl *
  1896. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1897. {
  1898. struct inode *inode = fhp->fh_dentry->d_inode;
  1899. char *name;
  1900. void *value = NULL;
  1901. ssize_t size;
  1902. struct posix_acl *acl;
  1903. if (!IS_POSIXACL(inode))
  1904. return ERR_PTR(-EOPNOTSUPP);
  1905. switch (type) {
  1906. case ACL_TYPE_ACCESS:
  1907. name = POSIX_ACL_XATTR_ACCESS;
  1908. break;
  1909. case ACL_TYPE_DEFAULT:
  1910. name = POSIX_ACL_XATTR_DEFAULT;
  1911. break;
  1912. default:
  1913. return ERR_PTR(-EOPNOTSUPP);
  1914. }
  1915. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1916. if (size < 0)
  1917. return ERR_PTR(size);
  1918. acl = posix_acl_from_xattr(value, size);
  1919. kfree(value);
  1920. return acl;
  1921. }
  1922. int
  1923. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1924. {
  1925. struct inode *inode = fhp->fh_dentry->d_inode;
  1926. char *name;
  1927. void *value = NULL;
  1928. size_t size;
  1929. int error;
  1930. if (!IS_POSIXACL(inode) ||
  1931. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1932. return -EOPNOTSUPP;
  1933. switch(type) {
  1934. case ACL_TYPE_ACCESS:
  1935. name = POSIX_ACL_XATTR_ACCESS;
  1936. break;
  1937. case ACL_TYPE_DEFAULT:
  1938. name = POSIX_ACL_XATTR_DEFAULT;
  1939. break;
  1940. default:
  1941. return -EOPNOTSUPP;
  1942. }
  1943. if (acl && acl->a_count) {
  1944. size = posix_acl_xattr_size(acl->a_count);
  1945. value = kmalloc(size, GFP_KERNEL);
  1946. if (!value)
  1947. return -ENOMEM;
  1948. error = posix_acl_to_xattr(acl, value, size);
  1949. if (error < 0)
  1950. goto getout;
  1951. size = error;
  1952. } else
  1953. size = 0;
  1954. error = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1955. if (error)
  1956. goto getout;
  1957. if (size)
  1958. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1959. else {
  1960. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1961. error = 0;
  1962. else {
  1963. error = vfs_removexattr(fhp->fh_dentry, name);
  1964. if (error == -ENODATA)
  1965. error = 0;
  1966. }
  1967. }
  1968. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1969. getout:
  1970. kfree(value);
  1971. return error;
  1972. }
  1973. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */