vfs.c 53 KB

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