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. while (d_mountpoint(path.dentry) && follow_down(&path))
  81. ;
  82. exp2 = rqst_exp_get_by_name(rqstp, &path);
  83. if (IS_ERR(exp2)) {
  84. err = PTR_ERR(exp2);
  85. /*
  86. * We normally allow NFS clients to continue
  87. * "underneath" a mountpoint that is not exported.
  88. * The exception is V4ROOT, where no traversal is ever
  89. * allowed without an explicit export of the new
  90. * directory.
  91. */
  92. if (err == -ENOENT && !(exp->ex_flags & NFSEXP_V4ROOT))
  93. err = 0;
  94. path_put(&path);
  95. goto out;
  96. }
  97. if (nfsd_v4client(rqstp) ||
  98. (exp->ex_flags & NFSEXP_CROSSMOUNT) || EX_NOHIDE(exp2)) {
  99. /* successfully crossed mount point */
  100. /*
  101. * This is subtle: path.dentry is *not* on path.mnt
  102. * at this point. The only reason we are safe is that
  103. * original mnt is pinned down by exp, so we should
  104. * put path *before* putting exp
  105. */
  106. *dpp = path.dentry;
  107. path.dentry = dentry;
  108. *expp = exp2;
  109. exp2 = exp;
  110. }
  111. path_put(&path);
  112. exp_put(exp2);
  113. out:
  114. return err;
  115. }
  116. static void follow_to_parent(struct path *path)
  117. {
  118. struct dentry *dp;
  119. while (path->dentry == path->mnt->mnt_root && follow_up(path))
  120. ;
  121. dp = dget_parent(path->dentry);
  122. dput(path->dentry);
  123. path->dentry = dp;
  124. }
  125. static int nfsd_lookup_parent(struct svc_rqst *rqstp, struct dentry *dparent, struct svc_export **exp, struct dentry **dentryp)
  126. {
  127. struct svc_export *exp2;
  128. struct path path = {.mnt = mntget((*exp)->ex_path.mnt),
  129. .dentry = dget(dparent)};
  130. follow_to_parent(&path);
  131. exp2 = rqst_exp_parent(rqstp, &path);
  132. if (PTR_ERR(exp2) == -ENOENT) {
  133. *dentryp = dget(dparent);
  134. } else if (IS_ERR(exp2)) {
  135. path_put(&path);
  136. return PTR_ERR(exp2);
  137. } else {
  138. *dentryp = dget(path.dentry);
  139. exp_put(*exp);
  140. *exp = exp2;
  141. }
  142. path_put(&path);
  143. return 0;
  144. }
  145. /*
  146. * For nfsd purposes, we treat V4ROOT exports as though there was an
  147. * export at *every* directory.
  148. */
  149. int nfsd_mountpoint(struct dentry *dentry, struct svc_export *exp)
  150. {
  151. if (d_mountpoint(dentry))
  152. return 1;
  153. if (!(exp->ex_flags & NFSEXP_V4ROOT))
  154. return 0;
  155. return dentry->d_inode != NULL;
  156. }
  157. __be32
  158. nfsd_lookup_dentry(struct svc_rqst *rqstp, struct svc_fh *fhp,
  159. const char *name, unsigned int len,
  160. struct svc_export **exp_ret, struct dentry **dentry_ret)
  161. {
  162. struct svc_export *exp;
  163. struct dentry *dparent;
  164. struct dentry *dentry;
  165. __be32 err;
  166. int host_err;
  167. dprintk("nfsd: nfsd_lookup(fh %s, %.*s)\n", SVCFH_fmt(fhp), len,name);
  168. /* Obtain dentry and export. */
  169. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_EXEC);
  170. if (err)
  171. return err;
  172. dparent = fhp->fh_dentry;
  173. exp = fhp->fh_export;
  174. exp_get(exp);
  175. /* Lookup the name, but don't follow links */
  176. if (isdotent(name, len)) {
  177. if (len==1)
  178. dentry = dget(dparent);
  179. else if (dparent != exp->ex_path.dentry)
  180. dentry = dget_parent(dparent);
  181. else if (!EX_NOHIDE(exp) && !nfsd_v4client(rqstp))
  182. dentry = dget(dparent); /* .. == . just like at / */
  183. else {
  184. /* checking mountpoint crossing is very different when stepping up */
  185. host_err = nfsd_lookup_parent(rqstp, dparent, &exp, &dentry);
  186. if (host_err)
  187. goto out_nfserr;
  188. }
  189. } else {
  190. fh_lock(fhp);
  191. dentry = lookup_one_len(name, dparent, len);
  192. host_err = PTR_ERR(dentry);
  193. if (IS_ERR(dentry))
  194. goto out_nfserr;
  195. /*
  196. * check if we have crossed a mount point ...
  197. */
  198. if (nfsd_mountpoint(dentry, exp)) {
  199. if ((host_err = nfsd_cross_mnt(rqstp, &dentry, &exp))) {
  200. dput(dentry);
  201. goto out_nfserr;
  202. }
  203. }
  204. }
  205. *dentry_ret = dentry;
  206. *exp_ret = exp;
  207. return 0;
  208. out_nfserr:
  209. exp_put(exp);
  210. return nfserrno(host_err);
  211. }
  212. /*
  213. * Look up one component of a pathname.
  214. * N.B. After this call _both_ fhp and resfh need an fh_put
  215. *
  216. * If the lookup would cross a mountpoint, and the mounted filesystem
  217. * is exported to the client with NFSEXP_NOHIDE, then the lookup is
  218. * accepted as it stands and the mounted directory is
  219. * returned. Otherwise the covered directory is returned.
  220. * NOTE: this mountpoint crossing is not supported properly by all
  221. * clients and is explicitly disallowed for NFSv3
  222. * NeilBrown <neilb@cse.unsw.edu.au>
  223. */
  224. __be32
  225. nfsd_lookup(struct svc_rqst *rqstp, struct svc_fh *fhp, const char *name,
  226. unsigned int len, struct svc_fh *resfh)
  227. {
  228. struct svc_export *exp;
  229. struct dentry *dentry;
  230. __be32 err;
  231. err = nfsd_lookup_dentry(rqstp, fhp, name, len, &exp, &dentry);
  232. if (err)
  233. return err;
  234. err = check_nfsd_access(exp, rqstp);
  235. if (err)
  236. goto out;
  237. /*
  238. * Note: we compose the file handle now, but as the
  239. * dentry may be negative, it may need to be updated.
  240. */
  241. err = fh_compose(resfh, exp, dentry, fhp);
  242. if (!err && !dentry->d_inode)
  243. err = nfserr_noent;
  244. out:
  245. dput(dentry);
  246. exp_put(exp);
  247. return err;
  248. }
  249. /*
  250. * Commit metadata changes to stable storage.
  251. */
  252. static int
  253. commit_metadata(struct svc_fh *fhp)
  254. {
  255. struct inode *inode = fhp->fh_dentry->d_inode;
  256. const struct export_operations *export_ops = inode->i_sb->s_export_op;
  257. if (!EX_ISSYNC(fhp->fh_export))
  258. return 0;
  259. if (export_ops->commit_metadata)
  260. return export_ops->commit_metadata(inode);
  261. return sync_inode_metadata(inode, 1);
  262. }
  263. /*
  264. * Set various file attributes.
  265. * N.B. After this call fhp needs an fh_put
  266. */
  267. __be32
  268. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  269. int check_guard, time_t guardtime)
  270. {
  271. struct dentry *dentry;
  272. struct inode *inode;
  273. int accmode = NFSD_MAY_SATTR;
  274. int ftype = 0;
  275. __be32 err;
  276. int host_err;
  277. int size_change = 0;
  278. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
  279. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  280. if (iap->ia_valid & ATTR_SIZE)
  281. ftype = S_IFREG;
  282. /* Get inode */
  283. err = fh_verify(rqstp, fhp, ftype, accmode);
  284. if (err)
  285. goto out;
  286. dentry = fhp->fh_dentry;
  287. inode = dentry->d_inode;
  288. /* Ignore any mode updates on symlinks */
  289. if (S_ISLNK(inode->i_mode))
  290. iap->ia_valid &= ~ATTR_MODE;
  291. if (!iap->ia_valid)
  292. goto out;
  293. /*
  294. * NFSv2 does not differentiate between "set-[ac]time-to-now"
  295. * which only requires access, and "set-[ac]time-to-X" which
  296. * requires ownership.
  297. * So if it looks like it might be "set both to the same time which
  298. * is close to now", and if inode_change_ok fails, then we
  299. * convert to "set to now" instead of "set to explicit time"
  300. *
  301. * We only call inode_change_ok as the last test as technically
  302. * it is not an interface that we should be using. It is only
  303. * valid if the filesystem does not define it's own i_op->setattr.
  304. */
  305. #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
  306. #define MAX_TOUCH_TIME_ERROR (30*60)
  307. if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
  308. iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
  309. /*
  310. * Looks probable.
  311. *
  312. * Now just make sure time is in the right ballpark.
  313. * Solaris, at least, doesn't seem to care what the time
  314. * request is. We require it be within 30 minutes of now.
  315. */
  316. time_t delta = iap->ia_atime.tv_sec - get_seconds();
  317. if (delta < 0)
  318. delta = -delta;
  319. if (delta < MAX_TOUCH_TIME_ERROR &&
  320. inode_change_ok(inode, iap) != 0) {
  321. /*
  322. * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
  323. * This will cause notify_change to set these times
  324. * to "now"
  325. */
  326. iap->ia_valid &= ~BOTH_TIME_SET;
  327. }
  328. }
  329. /*
  330. * The size case is special.
  331. * It changes the file as well as the attributes.
  332. */
  333. if (iap->ia_valid & ATTR_SIZE) {
  334. if (iap->ia_size < inode->i_size) {
  335. err = nfsd_permission(rqstp, fhp->fh_export, dentry,
  336. NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
  337. if (err)
  338. goto out;
  339. }
  340. /*
  341. * If we are changing the size of the file, then
  342. * we need to break all leases.
  343. */
  344. host_err = break_lease(inode, O_WRONLY | O_NONBLOCK);
  345. if (host_err == -EWOULDBLOCK)
  346. host_err = -ETIMEDOUT;
  347. if (host_err) /* ENOMEM or EWOULDBLOCK */
  348. goto out_nfserr;
  349. host_err = get_write_access(inode);
  350. if (host_err)
  351. goto out_nfserr;
  352. size_change = 1;
  353. host_err = locks_verify_truncate(inode, NULL, iap->ia_size);
  354. if (host_err) {
  355. put_write_access(inode);
  356. goto out_nfserr;
  357. }
  358. }
  359. /* sanitize the mode change */
  360. if (iap->ia_valid & ATTR_MODE) {
  361. iap->ia_mode &= S_IALLUGO;
  362. iap->ia_mode |= (inode->i_mode & ~S_IALLUGO);
  363. }
  364. /* Revoke setuid/setgid on chown */
  365. if (!S_ISDIR(inode->i_mode) &&
  366. (((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid) ||
  367. ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid))) {
  368. iap->ia_valid |= ATTR_KILL_PRIV;
  369. if (iap->ia_valid & ATTR_MODE) {
  370. /* we're setting mode too, just clear the s*id bits */
  371. iap->ia_mode &= ~S_ISUID;
  372. if (iap->ia_mode & S_IXGRP)
  373. iap->ia_mode &= ~S_ISGID;
  374. } else {
  375. /* set ATTR_KILL_* bits and let VFS handle it */
  376. iap->ia_valid |= (ATTR_KILL_SUID | ATTR_KILL_SGID);
  377. }
  378. }
  379. /* Change the attributes. */
  380. iap->ia_valid |= ATTR_CTIME;
  381. err = nfserr_notsync;
  382. if (!check_guard || guardtime == inode->i_ctime.tv_sec) {
  383. fh_lock(fhp);
  384. host_err = notify_change(dentry, iap);
  385. err = nfserrno(host_err);
  386. fh_unlock(fhp);
  387. }
  388. if (size_change)
  389. put_write_access(inode);
  390. if (!err)
  391. commit_metadata(fhp);
  392. out:
  393. return err;
  394. out_nfserr:
  395. err = nfserrno(host_err);
  396. goto out;
  397. }
  398. #if defined(CONFIG_NFSD_V2_ACL) || \
  399. defined(CONFIG_NFSD_V3_ACL) || \
  400. defined(CONFIG_NFSD_V4)
  401. static ssize_t nfsd_getxattr(struct dentry *dentry, char *key, void **buf)
  402. {
  403. ssize_t buflen;
  404. ssize_t ret;
  405. buflen = vfs_getxattr(dentry, key, NULL, 0);
  406. if (buflen <= 0)
  407. return buflen;
  408. *buf = kmalloc(buflen, GFP_KERNEL);
  409. if (!*buf)
  410. return -ENOMEM;
  411. ret = vfs_getxattr(dentry, key, *buf, buflen);
  412. if (ret < 0)
  413. kfree(*buf);
  414. return ret;
  415. }
  416. #endif
  417. #if defined(CONFIG_NFSD_V4)
  418. static int
  419. set_nfsv4_acl_one(struct dentry *dentry, struct posix_acl *pacl, char *key)
  420. {
  421. int len;
  422. size_t buflen;
  423. char *buf = NULL;
  424. int error = 0;
  425. buflen = posix_acl_xattr_size(pacl->a_count);
  426. buf = kmalloc(buflen, GFP_KERNEL);
  427. error = -ENOMEM;
  428. if (buf == NULL)
  429. goto out;
  430. len = posix_acl_to_xattr(pacl, buf, buflen);
  431. if (len < 0) {
  432. error = len;
  433. goto out;
  434. }
  435. error = vfs_setxattr(dentry, key, buf, len, 0);
  436. out:
  437. kfree(buf);
  438. return error;
  439. }
  440. __be32
  441. nfsd4_set_nfs4_acl(struct svc_rqst *rqstp, struct svc_fh *fhp,
  442. struct nfs4_acl *acl)
  443. {
  444. __be32 error;
  445. int host_error;
  446. struct dentry *dentry;
  447. struct inode *inode;
  448. struct posix_acl *pacl = NULL, *dpacl = NULL;
  449. unsigned int flags = 0;
  450. /* Get inode */
  451. error = fh_verify(rqstp, fhp, 0 /* S_IFREG */, NFSD_MAY_SATTR);
  452. if (error)
  453. return error;
  454. dentry = fhp->fh_dentry;
  455. inode = dentry->d_inode;
  456. if (S_ISDIR(inode->i_mode))
  457. flags = NFS4_ACL_DIR;
  458. host_error = nfs4_acl_nfsv4_to_posix(acl, &pacl, &dpacl, flags);
  459. if (host_error == -EINVAL) {
  460. return nfserr_attrnotsupp;
  461. } else if (host_error < 0)
  462. goto out_nfserr;
  463. host_error = set_nfsv4_acl_one(dentry, pacl, POSIX_ACL_XATTR_ACCESS);
  464. if (host_error < 0)
  465. goto out_release;
  466. if (S_ISDIR(inode->i_mode))
  467. host_error = set_nfsv4_acl_one(dentry, dpacl, POSIX_ACL_XATTR_DEFAULT);
  468. out_release:
  469. posix_acl_release(pacl);
  470. posix_acl_release(dpacl);
  471. out_nfserr:
  472. if (host_error == -EOPNOTSUPP)
  473. return nfserr_attrnotsupp;
  474. else
  475. return nfserrno(host_error);
  476. }
  477. static struct posix_acl *
  478. _get_posix_acl(struct dentry *dentry, char *key)
  479. {
  480. void *buf = NULL;
  481. struct posix_acl *pacl = NULL;
  482. int buflen;
  483. buflen = nfsd_getxattr(dentry, key, &buf);
  484. if (!buflen)
  485. buflen = -ENODATA;
  486. if (buflen <= 0)
  487. return ERR_PTR(buflen);
  488. pacl = posix_acl_from_xattr(buf, buflen);
  489. kfree(buf);
  490. return pacl;
  491. }
  492. int
  493. nfsd4_get_nfs4_acl(struct svc_rqst *rqstp, struct dentry *dentry, struct nfs4_acl **acl)
  494. {
  495. struct inode *inode = dentry->d_inode;
  496. int error = 0;
  497. struct posix_acl *pacl = NULL, *dpacl = NULL;
  498. unsigned int flags = 0;
  499. pacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_ACCESS);
  500. if (IS_ERR(pacl) && PTR_ERR(pacl) == -ENODATA)
  501. pacl = posix_acl_from_mode(inode->i_mode, GFP_KERNEL);
  502. if (IS_ERR(pacl)) {
  503. error = PTR_ERR(pacl);
  504. pacl = NULL;
  505. goto out;
  506. }
  507. if (S_ISDIR(inode->i_mode)) {
  508. dpacl = _get_posix_acl(dentry, POSIX_ACL_XATTR_DEFAULT);
  509. if (IS_ERR(dpacl) && PTR_ERR(dpacl) == -ENODATA)
  510. dpacl = NULL;
  511. else if (IS_ERR(dpacl)) {
  512. error = PTR_ERR(dpacl);
  513. dpacl = NULL;
  514. goto out;
  515. }
  516. flags = NFS4_ACL_DIR;
  517. }
  518. *acl = nfs4_acl_posix_to_nfsv4(pacl, dpacl, flags);
  519. if (IS_ERR(*acl)) {
  520. error = PTR_ERR(*acl);
  521. *acl = NULL;
  522. }
  523. out:
  524. posix_acl_release(pacl);
  525. posix_acl_release(dpacl);
  526. return error;
  527. }
  528. #endif /* defined(CONFIG_NFSD_V4) */
  529. #ifdef CONFIG_NFSD_V3
  530. /*
  531. * Check server access rights to a file system object
  532. */
  533. struct accessmap {
  534. u32 access;
  535. int how;
  536. };
  537. static struct accessmap nfs3_regaccess[] = {
  538. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  539. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  540. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_TRUNC },
  541. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE },
  542. { 0, 0 }
  543. };
  544. static struct accessmap nfs3_diraccess[] = {
  545. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  546. { NFS3_ACCESS_LOOKUP, NFSD_MAY_EXEC },
  547. { NFS3_ACCESS_MODIFY, NFSD_MAY_EXEC|NFSD_MAY_WRITE|NFSD_MAY_TRUNC},
  548. { NFS3_ACCESS_EXTEND, NFSD_MAY_EXEC|NFSD_MAY_WRITE },
  549. { NFS3_ACCESS_DELETE, NFSD_MAY_REMOVE },
  550. { 0, 0 }
  551. };
  552. static struct accessmap nfs3_anyaccess[] = {
  553. /* Some clients - Solaris 2.6 at least, make an access call
  554. * to the server to check for access for things like /dev/null
  555. * (which really, the server doesn't care about). So
  556. * We provide simple access checking for them, looking
  557. * mainly at mode bits, and we make sure to ignore read-only
  558. * filesystem checks
  559. */
  560. { NFS3_ACCESS_READ, NFSD_MAY_READ },
  561. { NFS3_ACCESS_EXECUTE, NFSD_MAY_EXEC },
  562. { NFS3_ACCESS_MODIFY, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  563. { NFS3_ACCESS_EXTEND, NFSD_MAY_WRITE|NFSD_MAY_LOCAL_ACCESS },
  564. { 0, 0 }
  565. };
  566. __be32
  567. nfsd_access(struct svc_rqst *rqstp, struct svc_fh *fhp, u32 *access, u32 *supported)
  568. {
  569. struct accessmap *map;
  570. struct svc_export *export;
  571. struct dentry *dentry;
  572. u32 query, result = 0, sresult = 0;
  573. __be32 error;
  574. error = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP);
  575. if (error)
  576. goto out;
  577. export = fhp->fh_export;
  578. dentry = fhp->fh_dentry;
  579. if (S_ISREG(dentry->d_inode->i_mode))
  580. map = nfs3_regaccess;
  581. else if (S_ISDIR(dentry->d_inode->i_mode))
  582. map = nfs3_diraccess;
  583. else
  584. map = nfs3_anyaccess;
  585. query = *access;
  586. for (; map->access; map++) {
  587. if (map->access & query) {
  588. __be32 err2;
  589. sresult |= map->access;
  590. err2 = nfsd_permission(rqstp, export, dentry, map->how);
  591. switch (err2) {
  592. case nfs_ok:
  593. result |= map->access;
  594. break;
  595. /* the following error codes just mean the access was not allowed,
  596. * rather than an error occurred */
  597. case nfserr_rofs:
  598. case nfserr_acces:
  599. case nfserr_perm:
  600. /* simply don't "or" in the access bit. */
  601. break;
  602. default:
  603. error = err2;
  604. goto out;
  605. }
  606. }
  607. }
  608. *access = result;
  609. if (supported)
  610. *supported = sresult;
  611. out:
  612. return error;
  613. }
  614. #endif /* CONFIG_NFSD_V3 */
  615. /*
  616. * Open an existing file or directory.
  617. * The access argument indicates the type of open (read/write/lock)
  618. * N.B. After this call fhp needs an fh_put
  619. */
  620. __be32
  621. nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  622. int access, struct file **filp)
  623. {
  624. struct dentry *dentry;
  625. struct inode *inode;
  626. int flags = O_RDONLY|O_LARGEFILE;
  627. __be32 err;
  628. int host_err = 0;
  629. validate_process_creds();
  630. /*
  631. * If we get here, then the client has already done an "open",
  632. * and (hopefully) checked permission - so allow OWNER_OVERRIDE
  633. * in case a chmod has now revoked permission.
  634. */
  635. err = fh_verify(rqstp, fhp, type, access | NFSD_MAY_OWNER_OVERRIDE);
  636. if (err)
  637. goto out;
  638. dentry = fhp->fh_dentry;
  639. inode = dentry->d_inode;
  640. /* Disallow write access to files with the append-only bit set
  641. * or any access when mandatory locking enabled
  642. */
  643. err = nfserr_perm;
  644. if (IS_APPEND(inode) && (access & NFSD_MAY_WRITE))
  645. goto out;
  646. /*
  647. * We must ignore files (but only files) which might have mandatory
  648. * locks on them because there is no way to know if the accesser has
  649. * the lock.
  650. */
  651. if (S_ISREG((inode)->i_mode) && mandatory_lock(inode))
  652. goto out;
  653. if (!inode->i_fop)
  654. goto out;
  655. /*
  656. * Check to see if there are any leases on this file.
  657. * This may block while leases are broken.
  658. */
  659. if (!(access & NFSD_MAY_NOT_BREAK_LEASE))
  660. host_err = break_lease(inode, O_NONBLOCK | ((access & NFSD_MAY_WRITE) ? O_WRONLY : 0));
  661. if (host_err == -EWOULDBLOCK)
  662. host_err = -ETIMEDOUT;
  663. if (host_err) /* NOMEM or WOULDBLOCK */
  664. goto out_nfserr;
  665. if (access & NFSD_MAY_WRITE) {
  666. if (access & NFSD_MAY_READ)
  667. flags = O_RDWR|O_LARGEFILE;
  668. else
  669. flags = O_WRONLY|O_LARGEFILE;
  670. }
  671. *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
  672. flags, current_cred());
  673. if (IS_ERR(*filp))
  674. host_err = PTR_ERR(*filp);
  675. else
  676. host_err = ima_file_check(*filp, access);
  677. out_nfserr:
  678. err = nfserrno(host_err);
  679. out:
  680. validate_process_creds();
  681. return err;
  682. }
  683. /*
  684. * Close a file.
  685. */
  686. void
  687. nfsd_close(struct file *filp)
  688. {
  689. fput(filp);
  690. }
  691. /*
  692. * Obtain the readahead parameters for the file
  693. * specified by (dev, ino).
  694. */
  695. static inline struct raparms *
  696. nfsd_get_raparms(dev_t dev, ino_t ino)
  697. {
  698. struct raparms *ra, **rap, **frap = NULL;
  699. int depth = 0;
  700. unsigned int hash;
  701. struct raparm_hbucket *rab;
  702. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  703. rab = &raparm_hash[hash];
  704. spin_lock(&rab->pb_lock);
  705. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  706. if (ra->p_ino == ino && ra->p_dev == dev)
  707. goto found;
  708. depth++;
  709. if (ra->p_count == 0)
  710. frap = rap;
  711. }
  712. depth = nfsdstats.ra_size*11/10;
  713. if (!frap) {
  714. spin_unlock(&rab->pb_lock);
  715. return NULL;
  716. }
  717. rap = frap;
  718. ra = *frap;
  719. ra->p_dev = dev;
  720. ra->p_ino = ino;
  721. ra->p_set = 0;
  722. ra->p_hindex = hash;
  723. found:
  724. if (rap != &rab->pb_head) {
  725. *rap = ra->p_next;
  726. ra->p_next = rab->pb_head;
  727. rab->pb_head = ra;
  728. }
  729. ra->p_count++;
  730. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  731. spin_unlock(&rab->pb_lock);
  732. return ra;
  733. }
  734. /*
  735. * Grab and keep cached pages associated with a file in the svc_rqst
  736. * so that they can be passed to the network sendmsg/sendpage routines
  737. * directly. They will be released after the sending has completed.
  738. */
  739. static int
  740. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  741. struct splice_desc *sd)
  742. {
  743. struct svc_rqst *rqstp = sd->u.data;
  744. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  745. struct page *page = buf->page;
  746. size_t size;
  747. size = sd->len;
  748. if (rqstp->rq_res.page_len == 0) {
  749. get_page(page);
  750. put_page(*pp);
  751. *pp = page;
  752. rqstp->rq_resused++;
  753. rqstp->rq_res.page_base = buf->offset;
  754. rqstp->rq_res.page_len = size;
  755. } else if (page != pp[-1]) {
  756. get_page(page);
  757. if (*pp)
  758. put_page(*pp);
  759. *pp = page;
  760. rqstp->rq_resused++;
  761. rqstp->rq_res.page_len += size;
  762. } else
  763. rqstp->rq_res.page_len += size;
  764. return size;
  765. }
  766. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  767. struct splice_desc *sd)
  768. {
  769. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  770. }
  771. static inline int svc_msnfs(struct svc_fh *ffhp)
  772. {
  773. #ifdef MSNFS
  774. return (ffhp->fh_export->ex_flags & NFSEXP_MSNFS);
  775. #else
  776. return 0;
  777. #endif
  778. }
  779. static __be32
  780. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  781. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  782. {
  783. struct inode *inode;
  784. mm_segment_t oldfs;
  785. __be32 err;
  786. int host_err;
  787. err = nfserr_perm;
  788. inode = file->f_path.dentry->d_inode;
  789. if (svc_msnfs(fhp) && !lock_may_read(inode, offset, *count))
  790. goto out;
  791. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  792. struct splice_desc sd = {
  793. .len = 0,
  794. .total_len = *count,
  795. .pos = offset,
  796. .u.data = rqstp,
  797. };
  798. rqstp->rq_resused = 1;
  799. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  800. } else {
  801. oldfs = get_fs();
  802. set_fs(KERNEL_DS);
  803. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  804. set_fs(oldfs);
  805. }
  806. if (host_err >= 0) {
  807. nfsdstats.io_read += host_err;
  808. *count = host_err;
  809. err = 0;
  810. fsnotify_access(file);
  811. } else
  812. err = nfserrno(host_err);
  813. out:
  814. return err;
  815. }
  816. static void kill_suid(struct dentry *dentry)
  817. {
  818. struct iattr ia;
  819. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  820. mutex_lock(&dentry->d_inode->i_mutex);
  821. notify_change(dentry, &ia);
  822. mutex_unlock(&dentry->d_inode->i_mutex);
  823. }
  824. /*
  825. * Gathered writes: If another process is currently writing to the file,
  826. * there's a high chance this is another nfsd (triggered by a bulk write
  827. * from a client's biod). Rather than syncing the file with each write
  828. * request, we sleep for 10 msec.
  829. *
  830. * I don't know if this roughly approximates C. Juszak's idea of
  831. * gathered writes, but it's a nice and simple solution (IMHO), and it
  832. * seems to work:-)
  833. *
  834. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  835. * better tool (separate unstable writes and commits) for solving this
  836. * problem.
  837. */
  838. static int wait_for_concurrent_writes(struct file *file)
  839. {
  840. struct inode *inode = file->f_path.dentry->d_inode;
  841. static ino_t last_ino;
  842. static dev_t last_dev;
  843. int err = 0;
  844. if (atomic_read(&inode->i_writecount) > 1
  845. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  846. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  847. msleep(10);
  848. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  849. }
  850. if (inode->i_state & I_DIRTY) {
  851. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  852. err = vfs_fsync(file, 0);
  853. }
  854. last_ino = inode->i_ino;
  855. last_dev = inode->i_sb->s_dev;
  856. return err;
  857. }
  858. static __be32
  859. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  860. loff_t offset, struct kvec *vec, int vlen,
  861. unsigned long *cnt, int *stablep)
  862. {
  863. struct svc_export *exp;
  864. struct dentry *dentry;
  865. struct inode *inode;
  866. mm_segment_t oldfs;
  867. __be32 err = 0;
  868. int host_err;
  869. int stable = *stablep;
  870. int use_wgather;
  871. #ifdef MSNFS
  872. err = nfserr_perm;
  873. if ((fhp->fh_export->ex_flags & NFSEXP_MSNFS) &&
  874. (!lock_may_write(file->f_path.dentry->d_inode, offset, *cnt)))
  875. goto out;
  876. #endif
  877. dentry = file->f_path.dentry;
  878. inode = dentry->d_inode;
  879. exp = fhp->fh_export;
  880. /*
  881. * Request sync writes if
  882. * - the sync export option has been set, or
  883. * - the client requested O_SYNC behavior (NFSv3 feature).
  884. * - The file system doesn't support fsync().
  885. * When NFSv2 gathered writes have been configured for this volume,
  886. * flushing the data to disk is handled separately below.
  887. */
  888. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  889. if (!file->f_op->fsync) {/* COMMIT3 cannot work */
  890. stable = 2;
  891. *stablep = 2; /* FILE_SYNC */
  892. }
  893. if (!EX_ISSYNC(exp))
  894. stable = 0;
  895. if (stable && !use_wgather) {
  896. spin_lock(&file->f_lock);
  897. file->f_flags |= O_SYNC;
  898. spin_unlock(&file->f_lock);
  899. }
  900. /* Write the data. */
  901. oldfs = get_fs(); set_fs(KERNEL_DS);
  902. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  903. set_fs(oldfs);
  904. if (host_err < 0)
  905. goto out_nfserr;
  906. *cnt = host_err;
  907. nfsdstats.io_write += host_err;
  908. fsnotify_modify(file);
  909. /* clear setuid/setgid flag after write */
  910. if (inode->i_mode & (S_ISUID | S_ISGID))
  911. kill_suid(dentry);
  912. if (stable && use_wgather)
  913. host_err = wait_for_concurrent_writes(file);
  914. out_nfserr:
  915. dprintk("nfsd: write complete host_err=%d\n", host_err);
  916. if (host_err >= 0)
  917. err = 0;
  918. else
  919. err = nfserrno(host_err);
  920. out:
  921. return err;
  922. }
  923. /*
  924. * Read data from a file. count must contain the requested read count
  925. * on entry. On return, *count contains the number of bytes actually read.
  926. * N.B. After this call fhp needs an fh_put
  927. */
  928. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  929. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  930. {
  931. struct file *file;
  932. struct inode *inode;
  933. struct raparms *ra;
  934. __be32 err;
  935. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  936. if (err)
  937. return err;
  938. inode = file->f_path.dentry->d_inode;
  939. /* Get readahead parameters */
  940. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  941. if (ra && ra->p_set)
  942. file->f_ra = ra->p_ra;
  943. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  944. /* Write back readahead params */
  945. if (ra) {
  946. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  947. spin_lock(&rab->pb_lock);
  948. ra->p_ra = file->f_ra;
  949. ra->p_set = 1;
  950. ra->p_count--;
  951. spin_unlock(&rab->pb_lock);
  952. }
  953. nfsd_close(file);
  954. return err;
  955. }
  956. /* As above, but use the provided file descriptor. */
  957. __be32
  958. nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  959. loff_t offset, struct kvec *vec, int vlen,
  960. unsigned long *count)
  961. {
  962. __be32 err;
  963. if (file) {
  964. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  965. NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
  966. if (err)
  967. goto out;
  968. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  969. } else /* Note file may still be NULL in NFSv4 special stateid case: */
  970. err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
  971. out:
  972. return err;
  973. }
  974. /*
  975. * Write data to a file.
  976. * The stable flag requests synchronous writes.
  977. * N.B. After this call fhp needs an fh_put
  978. */
  979. __be32
  980. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  981. loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
  982. int *stablep)
  983. {
  984. __be32 err = 0;
  985. if (file) {
  986. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  987. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  988. if (err)
  989. goto out;
  990. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  991. stablep);
  992. } else {
  993. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  994. if (err)
  995. goto out;
  996. if (cnt)
  997. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  998. cnt, stablep);
  999. nfsd_close(file);
  1000. }
  1001. out:
  1002. return err;
  1003. }
  1004. #ifdef CONFIG_NFSD_V3
  1005. /*
  1006. * Commit all pending writes to stable storage.
  1007. *
  1008. * Note: we only guarantee that data that lies within the range specified
  1009. * by the 'offset' and 'count' parameters will be synced.
  1010. *
  1011. * Unfortunately we cannot lock the file to make sure we return full WCC
  1012. * data to the client, as locking happens lower down in the filesystem.
  1013. */
  1014. __be32
  1015. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1016. loff_t offset, unsigned long count)
  1017. {
  1018. struct file *file;
  1019. loff_t end = LLONG_MAX;
  1020. __be32 err = nfserr_inval;
  1021. if (offset < 0)
  1022. goto out;
  1023. if (count != 0) {
  1024. end = offset + (loff_t)count - 1;
  1025. if (end < offset)
  1026. goto out;
  1027. }
  1028. err = nfsd_open(rqstp, fhp, S_IFREG,
  1029. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
  1030. if (err)
  1031. goto out;
  1032. if (EX_ISSYNC(fhp->fh_export)) {
  1033. int err2 = vfs_fsync_range(file, offset, end, 0);
  1034. if (err2 != -EINVAL)
  1035. err = nfserrno(err2);
  1036. else
  1037. err = nfserr_notsupp;
  1038. }
  1039. nfsd_close(file);
  1040. out:
  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. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1182. /*
  1183. * nfsd_setattr already committed the child. Transactional filesystems
  1184. * had a chance to commit changes for both parent and child
  1185. * simultaneously making the following commit_metadata a noop.
  1186. */
  1187. err2 = nfserrno(commit_metadata(fhp));
  1188. if (err2)
  1189. err = err2;
  1190. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1191. /*
  1192. * Update the file handle to get the new inode info.
  1193. */
  1194. if (!err)
  1195. err = fh_update(resfhp);
  1196. out:
  1197. if (dchild && !IS_ERR(dchild))
  1198. dput(dchild);
  1199. return err;
  1200. out_nfserr:
  1201. err = nfserrno(host_err);
  1202. goto out;
  1203. }
  1204. #ifdef CONFIG_NFSD_V3
  1205. /*
  1206. * NFSv3 version of nfsd_create
  1207. */
  1208. __be32
  1209. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1210. char *fname, int flen, struct iattr *iap,
  1211. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1212. int *truncp, int *created)
  1213. {
  1214. struct dentry *dentry, *dchild = NULL;
  1215. struct inode *dirp;
  1216. __be32 err;
  1217. int host_err;
  1218. __u32 v_mtime=0, v_atime=0;
  1219. err = nfserr_perm;
  1220. if (!flen)
  1221. goto out;
  1222. err = nfserr_exist;
  1223. if (isdotent(fname, flen))
  1224. goto out;
  1225. if (!(iap->ia_valid & ATTR_MODE))
  1226. iap->ia_mode = 0;
  1227. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1228. if (err)
  1229. goto out;
  1230. dentry = fhp->fh_dentry;
  1231. dirp = dentry->d_inode;
  1232. /* Get all the sanity checks out of the way before
  1233. * we lock the parent. */
  1234. err = nfserr_notdir;
  1235. if (!dirp->i_op->lookup)
  1236. goto out;
  1237. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1238. /*
  1239. * Compose the response file handle.
  1240. */
  1241. dchild = lookup_one_len(fname, dentry, flen);
  1242. host_err = PTR_ERR(dchild);
  1243. if (IS_ERR(dchild))
  1244. goto out_nfserr;
  1245. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1246. if (err)
  1247. goto out;
  1248. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1249. /* solaris7 gets confused (bugid 4218508) if these have
  1250. * the high bit set, so just clear the high bits. If this is
  1251. * ever changed to use different attrs for storing the
  1252. * verifier, then do_open_lookup() will also need to be fixed
  1253. * accordingly.
  1254. */
  1255. v_mtime = verifier[0]&0x7fffffff;
  1256. v_atime = verifier[1]&0x7fffffff;
  1257. }
  1258. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1259. if (host_err)
  1260. goto out_nfserr;
  1261. if (dchild->d_inode) {
  1262. err = 0;
  1263. switch (createmode) {
  1264. case NFS3_CREATE_UNCHECKED:
  1265. if (! S_ISREG(dchild->d_inode->i_mode))
  1266. err = nfserr_exist;
  1267. else if (truncp) {
  1268. /* in nfsv4, we need to treat this case a little
  1269. * differently. we don't want to truncate the
  1270. * file now; this would be wrong if the OPEN
  1271. * fails for some other reason. furthermore,
  1272. * if the size is nonzero, we should ignore it
  1273. * according to spec!
  1274. */
  1275. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1276. }
  1277. else {
  1278. iap->ia_valid &= ATTR_SIZE;
  1279. goto set_attr;
  1280. }
  1281. break;
  1282. case NFS3_CREATE_EXCLUSIVE:
  1283. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1284. && dchild->d_inode->i_atime.tv_sec == v_atime
  1285. && dchild->d_inode->i_size == 0 )
  1286. break;
  1287. /* fallthru */
  1288. case NFS3_CREATE_GUARDED:
  1289. err = nfserr_exist;
  1290. }
  1291. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1292. goto out;
  1293. }
  1294. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1295. if (host_err < 0) {
  1296. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1297. goto out_nfserr;
  1298. }
  1299. if (created)
  1300. *created = 1;
  1301. nfsd_check_ignore_resizing(iap);
  1302. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1303. /* Cram the verifier into atime/mtime */
  1304. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1305. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1306. /* XXX someone who knows this better please fix it for nsec */
  1307. iap->ia_mtime.tv_sec = v_mtime;
  1308. iap->ia_atime.tv_sec = v_atime;
  1309. iap->ia_mtime.tv_nsec = 0;
  1310. iap->ia_atime.tv_nsec = 0;
  1311. }
  1312. set_attr:
  1313. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1314. /*
  1315. * nfsd_setattr already committed the child (and possibly also the parent).
  1316. */
  1317. if (!err)
  1318. err = nfserrno(commit_metadata(fhp));
  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. err = nfserrno(host_err);
  1418. if (!err)
  1419. err = nfserrno(commit_metadata(fhp));
  1420. fh_unlock(fhp);
  1421. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1422. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1423. dput(dnew);
  1424. if (err==0) err = cerr;
  1425. out:
  1426. return err;
  1427. out_nfserr:
  1428. err = nfserrno(host_err);
  1429. goto out;
  1430. }
  1431. /*
  1432. * Create a hardlink
  1433. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1434. */
  1435. __be32
  1436. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1437. char *name, int len, struct svc_fh *tfhp)
  1438. {
  1439. struct dentry *ddir, *dnew, *dold;
  1440. struct inode *dirp;
  1441. __be32 err;
  1442. int host_err;
  1443. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1444. if (err)
  1445. goto out;
  1446. err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
  1447. if (err)
  1448. goto out;
  1449. err = nfserr_perm;
  1450. if (!len)
  1451. goto out;
  1452. err = nfserr_exist;
  1453. if (isdotent(name, len))
  1454. goto out;
  1455. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1456. ddir = ffhp->fh_dentry;
  1457. dirp = ddir->d_inode;
  1458. dnew = lookup_one_len(name, ddir, len);
  1459. host_err = PTR_ERR(dnew);
  1460. if (IS_ERR(dnew))
  1461. goto out_nfserr;
  1462. dold = tfhp->fh_dentry;
  1463. host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
  1464. if (host_err) {
  1465. err = nfserrno(host_err);
  1466. goto out_dput;
  1467. }
  1468. host_err = vfs_link(dold, dirp, dnew);
  1469. if (!host_err) {
  1470. err = nfserrno(commit_metadata(ffhp));
  1471. if (!err)
  1472. err = nfserrno(commit_metadata(tfhp));
  1473. } else {
  1474. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1475. err = nfserr_acces;
  1476. else
  1477. err = nfserrno(host_err);
  1478. }
  1479. mnt_drop_write(tfhp->fh_export->ex_path.mnt);
  1480. out_dput:
  1481. dput(dnew);
  1482. out_unlock:
  1483. fh_unlock(ffhp);
  1484. out:
  1485. return err;
  1486. out_nfserr:
  1487. err = nfserrno(host_err);
  1488. goto out_unlock;
  1489. }
  1490. /*
  1491. * Rename a file
  1492. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1493. */
  1494. __be32
  1495. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1496. struct svc_fh *tfhp, char *tname, int tlen)
  1497. {
  1498. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1499. struct inode *fdir, *tdir;
  1500. __be32 err;
  1501. int host_err;
  1502. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1503. if (err)
  1504. goto out;
  1505. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1506. if (err)
  1507. goto out;
  1508. fdentry = ffhp->fh_dentry;
  1509. fdir = fdentry->d_inode;
  1510. tdentry = tfhp->fh_dentry;
  1511. tdir = tdentry->d_inode;
  1512. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1513. if (ffhp->fh_export != tfhp->fh_export)
  1514. goto out;
  1515. err = nfserr_perm;
  1516. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1517. goto out;
  1518. /* cannot use fh_lock as we need deadlock protective ordering
  1519. * so do it by hand */
  1520. trap = lock_rename(tdentry, fdentry);
  1521. ffhp->fh_locked = tfhp->fh_locked = 1;
  1522. fill_pre_wcc(ffhp);
  1523. fill_pre_wcc(tfhp);
  1524. odentry = lookup_one_len(fname, fdentry, flen);
  1525. host_err = PTR_ERR(odentry);
  1526. if (IS_ERR(odentry))
  1527. goto out_nfserr;
  1528. host_err = -ENOENT;
  1529. if (!odentry->d_inode)
  1530. goto out_dput_old;
  1531. host_err = -EINVAL;
  1532. if (odentry == trap)
  1533. goto out_dput_old;
  1534. ndentry = lookup_one_len(tname, tdentry, tlen);
  1535. host_err = PTR_ERR(ndentry);
  1536. if (IS_ERR(ndentry))
  1537. goto out_dput_old;
  1538. host_err = -ENOTEMPTY;
  1539. if (ndentry == trap)
  1540. goto out_dput_new;
  1541. if (svc_msnfs(ffhp) &&
  1542. ((atomic_read(&odentry->d_count) > 1)
  1543. || (atomic_read(&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. (atomic_read(&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) */