vfs.c 53 KB

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