vfs.c 54 KB

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