vfs.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255
  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. err = follow_down(&path, false);
  80. if (err < 0)
  81. goto out;
  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. static int nfsd_break_lease(struct inode *inode)
  250. {
  251. if (!S_ISREG(inode->i_mode))
  252. return 0;
  253. return break_lease(inode, O_WRONLY | O_NONBLOCK);
  254. }
  255. /*
  256. * Commit metadata changes to stable storage.
  257. */
  258. static int
  259. commit_metadata(struct svc_fh *fhp)
  260. {
  261. struct inode *inode = fhp->fh_dentry->d_inode;
  262. const struct export_operations *export_ops = inode->i_sb->s_export_op;
  263. if (!EX_ISSYNC(fhp->fh_export))
  264. return 0;
  265. if (export_ops->commit_metadata)
  266. return export_ops->commit_metadata(inode);
  267. return sync_inode_metadata(inode, 1);
  268. }
  269. /*
  270. * Set various file attributes.
  271. * N.B. After this call fhp needs an fh_put
  272. */
  273. __be32
  274. nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
  275. int check_guard, time_t guardtime)
  276. {
  277. struct dentry *dentry;
  278. struct inode *inode;
  279. int accmode = NFSD_MAY_SATTR;
  280. int ftype = 0;
  281. __be32 err;
  282. int host_err;
  283. int size_change = 0;
  284. if (iap->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_SIZE))
  285. accmode |= NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE;
  286. if (iap->ia_valid & ATTR_SIZE)
  287. ftype = S_IFREG;
  288. /* Get inode */
  289. err = fh_verify(rqstp, fhp, ftype, accmode);
  290. if (err)
  291. goto out;
  292. dentry = fhp->fh_dentry;
  293. inode = dentry->d_inode;
  294. /* Ignore any mode updates on symlinks */
  295. if (S_ISLNK(inode->i_mode))
  296. iap->ia_valid &= ~ATTR_MODE;
  297. if (!iap->ia_valid)
  298. goto out;
  299. /*
  300. * NFSv2 does not differentiate between "set-[ac]time-to-now"
  301. * which only requires access, and "set-[ac]time-to-X" which
  302. * requires ownership.
  303. * So if it looks like it might be "set both to the same time which
  304. * is close to now", and if inode_change_ok fails, then we
  305. * convert to "set to now" instead of "set to explicit time"
  306. *
  307. * We only call inode_change_ok as the last test as technically
  308. * it is not an interface that we should be using. It is only
  309. * valid if the filesystem does not define it's own i_op->setattr.
  310. */
  311. #define BOTH_TIME_SET (ATTR_ATIME_SET | ATTR_MTIME_SET)
  312. #define MAX_TOUCH_TIME_ERROR (30*60)
  313. if ((iap->ia_valid & BOTH_TIME_SET) == BOTH_TIME_SET &&
  314. iap->ia_mtime.tv_sec == iap->ia_atime.tv_sec) {
  315. /*
  316. * Looks probable.
  317. *
  318. * Now just make sure time is in the right ballpark.
  319. * Solaris, at least, doesn't seem to care what the time
  320. * request is. We require it be within 30 minutes of now.
  321. */
  322. time_t delta = iap->ia_atime.tv_sec - get_seconds();
  323. if (delta < 0)
  324. delta = -delta;
  325. if (delta < MAX_TOUCH_TIME_ERROR &&
  326. inode_change_ok(inode, iap) != 0) {
  327. /*
  328. * Turn off ATTR_[AM]TIME_SET but leave ATTR_[AM]TIME.
  329. * This will cause notify_change to set these times
  330. * to "now"
  331. */
  332. iap->ia_valid &= ~BOTH_TIME_SET;
  333. }
  334. }
  335. /*
  336. * The size case is special.
  337. * It changes the file as well as the attributes.
  338. */
  339. if (iap->ia_valid & ATTR_SIZE) {
  340. if (iap->ia_size < inode->i_size) {
  341. err = nfsd_permission(rqstp, fhp->fh_export, dentry,
  342. NFSD_MAY_TRUNC|NFSD_MAY_OWNER_OVERRIDE);
  343. if (err)
  344. goto out;
  345. }
  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. host_err = nfsd_break_lease(inode);
  381. if (host_err)
  382. goto out_nfserr;
  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) /* NOMEM or WOULDBLOCK */
  662. goto out_nfserr;
  663. if (access & NFSD_MAY_WRITE) {
  664. if (access & NFSD_MAY_READ)
  665. flags = O_RDWR|O_LARGEFILE;
  666. else
  667. flags = O_WRONLY|O_LARGEFILE;
  668. }
  669. *filp = dentry_open(dget(dentry), mntget(fhp->fh_export->ex_path.mnt),
  670. flags, current_cred());
  671. if (IS_ERR(*filp))
  672. host_err = PTR_ERR(*filp);
  673. else
  674. host_err = ima_file_check(*filp, access);
  675. out_nfserr:
  676. err = nfserrno(host_err);
  677. out:
  678. validate_process_creds();
  679. return err;
  680. }
  681. /*
  682. * Close a file.
  683. */
  684. void
  685. nfsd_close(struct file *filp)
  686. {
  687. fput(filp);
  688. }
  689. /*
  690. * Obtain the readahead parameters for the file
  691. * specified by (dev, ino).
  692. */
  693. static inline struct raparms *
  694. nfsd_get_raparms(dev_t dev, ino_t ino)
  695. {
  696. struct raparms *ra, **rap, **frap = NULL;
  697. int depth = 0;
  698. unsigned int hash;
  699. struct raparm_hbucket *rab;
  700. hash = jhash_2words(dev, ino, 0xfeedbeef) & RAPARM_HASH_MASK;
  701. rab = &raparm_hash[hash];
  702. spin_lock(&rab->pb_lock);
  703. for (rap = &rab->pb_head; (ra = *rap); rap = &ra->p_next) {
  704. if (ra->p_ino == ino && ra->p_dev == dev)
  705. goto found;
  706. depth++;
  707. if (ra->p_count == 0)
  708. frap = rap;
  709. }
  710. depth = nfsdstats.ra_size;
  711. if (!frap) {
  712. spin_unlock(&rab->pb_lock);
  713. return NULL;
  714. }
  715. rap = frap;
  716. ra = *frap;
  717. ra->p_dev = dev;
  718. ra->p_ino = ino;
  719. ra->p_set = 0;
  720. ra->p_hindex = hash;
  721. found:
  722. if (rap != &rab->pb_head) {
  723. *rap = ra->p_next;
  724. ra->p_next = rab->pb_head;
  725. rab->pb_head = ra;
  726. }
  727. ra->p_count++;
  728. nfsdstats.ra_depth[depth*10/nfsdstats.ra_size]++;
  729. spin_unlock(&rab->pb_lock);
  730. return ra;
  731. }
  732. /*
  733. * Grab and keep cached pages associated with a file in the svc_rqst
  734. * so that they can be passed to the network sendmsg/sendpage routines
  735. * directly. They will be released after the sending has completed.
  736. */
  737. static int
  738. nfsd_splice_actor(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
  739. struct splice_desc *sd)
  740. {
  741. struct svc_rqst *rqstp = sd->u.data;
  742. struct page **pp = rqstp->rq_respages + rqstp->rq_resused;
  743. struct page *page = buf->page;
  744. size_t size;
  745. size = sd->len;
  746. if (rqstp->rq_res.page_len == 0) {
  747. get_page(page);
  748. put_page(*pp);
  749. *pp = page;
  750. rqstp->rq_resused++;
  751. rqstp->rq_res.page_base = buf->offset;
  752. rqstp->rq_res.page_len = size;
  753. } else if (page != pp[-1]) {
  754. get_page(page);
  755. if (*pp)
  756. put_page(*pp);
  757. *pp = page;
  758. rqstp->rq_resused++;
  759. rqstp->rq_res.page_len += size;
  760. } else
  761. rqstp->rq_res.page_len += size;
  762. return size;
  763. }
  764. static int nfsd_direct_splice_actor(struct pipe_inode_info *pipe,
  765. struct splice_desc *sd)
  766. {
  767. return __splice_from_pipe(pipe, sd, nfsd_splice_actor);
  768. }
  769. static __be32
  770. nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  771. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  772. {
  773. struct inode *inode;
  774. mm_segment_t oldfs;
  775. __be32 err;
  776. int host_err;
  777. err = nfserr_perm;
  778. inode = file->f_path.dentry->d_inode;
  779. if (file->f_op->splice_read && rqstp->rq_splice_ok) {
  780. struct splice_desc sd = {
  781. .len = 0,
  782. .total_len = *count,
  783. .pos = offset,
  784. .u.data = rqstp,
  785. };
  786. rqstp->rq_resused = 1;
  787. host_err = splice_direct_to_actor(file, &sd, nfsd_direct_splice_actor);
  788. } else {
  789. oldfs = get_fs();
  790. set_fs(KERNEL_DS);
  791. host_err = vfs_readv(file, (struct iovec __user *)vec, vlen, &offset);
  792. set_fs(oldfs);
  793. }
  794. if (host_err >= 0) {
  795. nfsdstats.io_read += host_err;
  796. *count = host_err;
  797. err = 0;
  798. fsnotify_access(file);
  799. } else
  800. err = nfserrno(host_err);
  801. return err;
  802. }
  803. static void kill_suid(struct dentry *dentry)
  804. {
  805. struct iattr ia;
  806. ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
  807. mutex_lock(&dentry->d_inode->i_mutex);
  808. notify_change(dentry, &ia);
  809. mutex_unlock(&dentry->d_inode->i_mutex);
  810. }
  811. /*
  812. * Gathered writes: If another process is currently writing to the file,
  813. * there's a high chance this is another nfsd (triggered by a bulk write
  814. * from a client's biod). Rather than syncing the file with each write
  815. * request, we sleep for 10 msec.
  816. *
  817. * I don't know if this roughly approximates C. Juszak's idea of
  818. * gathered writes, but it's a nice and simple solution (IMHO), and it
  819. * seems to work:-)
  820. *
  821. * Note: we do this only in the NFSv2 case, since v3 and higher have a
  822. * better tool (separate unstable writes and commits) for solving this
  823. * problem.
  824. */
  825. static int wait_for_concurrent_writes(struct file *file)
  826. {
  827. struct inode *inode = file->f_path.dentry->d_inode;
  828. static ino_t last_ino;
  829. static dev_t last_dev;
  830. int err = 0;
  831. if (atomic_read(&inode->i_writecount) > 1
  832. || (last_ino == inode->i_ino && last_dev == inode->i_sb->s_dev)) {
  833. dprintk("nfsd: write defer %d\n", task_pid_nr(current));
  834. msleep(10);
  835. dprintk("nfsd: write resume %d\n", task_pid_nr(current));
  836. }
  837. if (inode->i_state & I_DIRTY) {
  838. dprintk("nfsd: write sync %d\n", task_pid_nr(current));
  839. err = vfs_fsync(file, 0);
  840. }
  841. last_ino = inode->i_ino;
  842. last_dev = inode->i_sb->s_dev;
  843. return err;
  844. }
  845. static __be32
  846. nfsd_vfs_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  847. loff_t offset, struct kvec *vec, int vlen,
  848. unsigned long *cnt, int *stablep)
  849. {
  850. struct svc_export *exp;
  851. struct dentry *dentry;
  852. struct inode *inode;
  853. mm_segment_t oldfs;
  854. __be32 err = 0;
  855. int host_err;
  856. int stable = *stablep;
  857. int use_wgather;
  858. dentry = file->f_path.dentry;
  859. inode = dentry->d_inode;
  860. exp = fhp->fh_export;
  861. /*
  862. * Request sync writes if
  863. * - the sync export option has been set, or
  864. * - the client requested O_SYNC behavior (NFSv3 feature).
  865. * - The file system doesn't support fsync().
  866. * When NFSv2 gathered writes have been configured for this volume,
  867. * flushing the data to disk is handled separately below.
  868. */
  869. use_wgather = (rqstp->rq_vers == 2) && EX_WGATHER(exp);
  870. if (!file->f_op->fsync) {/* COMMIT3 cannot work */
  871. stable = 2;
  872. *stablep = 2; /* FILE_SYNC */
  873. }
  874. if (!EX_ISSYNC(exp))
  875. stable = 0;
  876. if (stable && !use_wgather) {
  877. spin_lock(&file->f_lock);
  878. file->f_flags |= O_SYNC;
  879. spin_unlock(&file->f_lock);
  880. }
  881. /* Write the data. */
  882. oldfs = get_fs(); set_fs(KERNEL_DS);
  883. host_err = vfs_writev(file, (struct iovec __user *)vec, vlen, &offset);
  884. set_fs(oldfs);
  885. if (host_err < 0)
  886. goto out_nfserr;
  887. *cnt = host_err;
  888. nfsdstats.io_write += host_err;
  889. fsnotify_modify(file);
  890. /* clear setuid/setgid flag after write */
  891. if (inode->i_mode & (S_ISUID | S_ISGID))
  892. kill_suid(dentry);
  893. if (stable && use_wgather)
  894. host_err = wait_for_concurrent_writes(file);
  895. out_nfserr:
  896. dprintk("nfsd: write complete host_err=%d\n", host_err);
  897. if (host_err >= 0)
  898. err = 0;
  899. else
  900. err = nfserrno(host_err);
  901. return err;
  902. }
  903. /*
  904. * Read data from a file. count must contain the requested read count
  905. * on entry. On return, *count contains the number of bytes actually read.
  906. * N.B. After this call fhp needs an fh_put
  907. */
  908. __be32 nfsd_read(struct svc_rqst *rqstp, struct svc_fh *fhp,
  909. loff_t offset, struct kvec *vec, int vlen, unsigned long *count)
  910. {
  911. struct file *file;
  912. struct inode *inode;
  913. struct raparms *ra;
  914. __be32 err;
  915. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
  916. if (err)
  917. return err;
  918. inode = file->f_path.dentry->d_inode;
  919. /* Get readahead parameters */
  920. ra = nfsd_get_raparms(inode->i_sb->s_dev, inode->i_ino);
  921. if (ra && ra->p_set)
  922. file->f_ra = ra->p_ra;
  923. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  924. /* Write back readahead params */
  925. if (ra) {
  926. struct raparm_hbucket *rab = &raparm_hash[ra->p_hindex];
  927. spin_lock(&rab->pb_lock);
  928. ra->p_ra = file->f_ra;
  929. ra->p_set = 1;
  930. ra->p_count--;
  931. spin_unlock(&rab->pb_lock);
  932. }
  933. nfsd_close(file);
  934. return err;
  935. }
  936. /* As above, but use the provided file descriptor. */
  937. __be32
  938. nfsd_read_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  939. loff_t offset, struct kvec *vec, int vlen,
  940. unsigned long *count)
  941. {
  942. __be32 err;
  943. if (file) {
  944. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  945. NFSD_MAY_READ|NFSD_MAY_OWNER_OVERRIDE);
  946. if (err)
  947. goto out;
  948. err = nfsd_vfs_read(rqstp, fhp, file, offset, vec, vlen, count);
  949. } else /* Note file may still be NULL in NFSv4 special stateid case: */
  950. err = nfsd_read(rqstp, fhp, offset, vec, vlen, count);
  951. out:
  952. return err;
  953. }
  954. /*
  955. * Write data to a file.
  956. * The stable flag requests synchronous writes.
  957. * N.B. After this call fhp needs an fh_put
  958. */
  959. __be32
  960. nfsd_write(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
  961. loff_t offset, struct kvec *vec, int vlen, unsigned long *cnt,
  962. int *stablep)
  963. {
  964. __be32 err = 0;
  965. if (file) {
  966. err = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
  967. NFSD_MAY_WRITE|NFSD_MAY_OWNER_OVERRIDE);
  968. if (err)
  969. goto out;
  970. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen, cnt,
  971. stablep);
  972. } else {
  973. err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_WRITE, &file);
  974. if (err)
  975. goto out;
  976. if (cnt)
  977. err = nfsd_vfs_write(rqstp, fhp, file, offset, vec, vlen,
  978. cnt, stablep);
  979. nfsd_close(file);
  980. }
  981. out:
  982. return err;
  983. }
  984. #ifdef CONFIG_NFSD_V3
  985. /*
  986. * Commit all pending writes to stable storage.
  987. *
  988. * Note: we only guarantee that data that lies within the range specified
  989. * by the 'offset' and 'count' parameters will be synced.
  990. *
  991. * Unfortunately we cannot lock the file to make sure we return full WCC
  992. * data to the client, as locking happens lower down in the filesystem.
  993. */
  994. __be32
  995. nfsd_commit(struct svc_rqst *rqstp, struct svc_fh *fhp,
  996. loff_t offset, unsigned long count)
  997. {
  998. struct file *file;
  999. loff_t end = LLONG_MAX;
  1000. __be32 err = nfserr_inval;
  1001. if (offset < 0)
  1002. goto out;
  1003. if (count != 0) {
  1004. end = offset + (loff_t)count - 1;
  1005. if (end < offset)
  1006. goto out;
  1007. }
  1008. err = nfsd_open(rqstp, fhp, S_IFREG,
  1009. NFSD_MAY_WRITE|NFSD_MAY_NOT_BREAK_LEASE, &file);
  1010. if (err)
  1011. goto out;
  1012. if (EX_ISSYNC(fhp->fh_export)) {
  1013. int err2 = vfs_fsync_range(file, offset, end, 0);
  1014. if (err2 != -EINVAL)
  1015. err = nfserrno(err2);
  1016. else
  1017. err = nfserr_notsupp;
  1018. }
  1019. nfsd_close(file);
  1020. out:
  1021. return err;
  1022. }
  1023. #endif /* CONFIG_NFSD_V3 */
  1024. static __be32
  1025. nfsd_create_setattr(struct svc_rqst *rqstp, struct svc_fh *resfhp,
  1026. struct iattr *iap)
  1027. {
  1028. /*
  1029. * Mode has already been set earlier in create:
  1030. */
  1031. iap->ia_valid &= ~ATTR_MODE;
  1032. /*
  1033. * Setting uid/gid works only for root. Irix appears to
  1034. * send along the gid on create when it tries to implement
  1035. * setgid directories via NFS:
  1036. */
  1037. if (current_fsuid() != 0)
  1038. iap->ia_valid &= ~(ATTR_UID|ATTR_GID);
  1039. if (iap->ia_valid)
  1040. return nfsd_setattr(rqstp, resfhp, iap, 0, (time_t)0);
  1041. return 0;
  1042. }
  1043. /* HPUX client sometimes creates a file in mode 000, and sets size to 0.
  1044. * setting size to 0 may fail for some specific file systems by the permission
  1045. * checking which requires WRITE permission but the mode is 000.
  1046. * we ignore the resizing(to 0) on the just new created file, since the size is
  1047. * 0 after file created.
  1048. *
  1049. * call this only after vfs_create() is called.
  1050. * */
  1051. static void
  1052. nfsd_check_ignore_resizing(struct iattr *iap)
  1053. {
  1054. if ((iap->ia_valid & ATTR_SIZE) && (iap->ia_size == 0))
  1055. iap->ia_valid &= ~ATTR_SIZE;
  1056. }
  1057. /*
  1058. * Create a file (regular, directory, device, fifo); UNIX sockets
  1059. * not yet implemented.
  1060. * If the response fh has been verified, the parent directory should
  1061. * already be locked. Note that the parent directory is left locked.
  1062. *
  1063. * N.B. Every call to nfsd_create needs an fh_put for _both_ fhp and resfhp
  1064. */
  1065. __be32
  1066. nfsd_create(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1067. char *fname, int flen, struct iattr *iap,
  1068. int type, dev_t rdev, struct svc_fh *resfhp)
  1069. {
  1070. struct dentry *dentry, *dchild = NULL;
  1071. struct inode *dirp;
  1072. __be32 err;
  1073. __be32 err2;
  1074. int host_err;
  1075. err = nfserr_perm;
  1076. if (!flen)
  1077. goto out;
  1078. err = nfserr_exist;
  1079. if (isdotent(fname, flen))
  1080. goto out;
  1081. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1082. if (err)
  1083. goto out;
  1084. dentry = fhp->fh_dentry;
  1085. dirp = dentry->d_inode;
  1086. err = nfserr_notdir;
  1087. if (!dirp->i_op->lookup)
  1088. goto out;
  1089. /*
  1090. * Check whether the response file handle has been verified yet.
  1091. * If it has, the parent directory should already be locked.
  1092. */
  1093. if (!resfhp->fh_dentry) {
  1094. /* called from nfsd_proc_mkdir, or possibly nfsd3_proc_create */
  1095. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1096. dchild = lookup_one_len(fname, dentry, flen);
  1097. host_err = PTR_ERR(dchild);
  1098. if (IS_ERR(dchild))
  1099. goto out_nfserr;
  1100. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1101. if (err)
  1102. goto out;
  1103. } else {
  1104. /* called from nfsd_proc_create */
  1105. dchild = dget(resfhp->fh_dentry);
  1106. if (!fhp->fh_locked) {
  1107. /* not actually possible */
  1108. printk(KERN_ERR
  1109. "nfsd_create: parent %s/%s not locked!\n",
  1110. dentry->d_parent->d_name.name,
  1111. dentry->d_name.name);
  1112. err = nfserr_io;
  1113. goto out;
  1114. }
  1115. }
  1116. /*
  1117. * Make sure the child dentry is still negative ...
  1118. */
  1119. err = nfserr_exist;
  1120. if (dchild->d_inode) {
  1121. dprintk("nfsd_create: dentry %s/%s not negative!\n",
  1122. dentry->d_name.name, dchild->d_name.name);
  1123. goto out;
  1124. }
  1125. if (!(iap->ia_valid & ATTR_MODE))
  1126. iap->ia_mode = 0;
  1127. iap->ia_mode = (iap->ia_mode & S_IALLUGO) | type;
  1128. err = nfserr_inval;
  1129. if (!S_ISREG(type) && !S_ISDIR(type) && !special_file(type)) {
  1130. printk(KERN_WARNING "nfsd: bad file type %o in nfsd_create\n",
  1131. type);
  1132. goto out;
  1133. }
  1134. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1135. if (host_err)
  1136. goto out_nfserr;
  1137. /*
  1138. * Get the dir op function pointer.
  1139. */
  1140. err = 0;
  1141. switch (type) {
  1142. case S_IFREG:
  1143. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1144. if (!host_err)
  1145. nfsd_check_ignore_resizing(iap);
  1146. break;
  1147. case S_IFDIR:
  1148. host_err = vfs_mkdir(dirp, dchild, iap->ia_mode);
  1149. break;
  1150. case S_IFCHR:
  1151. case S_IFBLK:
  1152. case S_IFIFO:
  1153. case S_IFSOCK:
  1154. host_err = vfs_mknod(dirp, dchild, iap->ia_mode, rdev);
  1155. break;
  1156. }
  1157. if (host_err < 0) {
  1158. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1159. goto out_nfserr;
  1160. }
  1161. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1162. /*
  1163. * nfsd_setattr already committed the child. Transactional filesystems
  1164. * had a chance to commit changes for both parent and child
  1165. * simultaneously making the following commit_metadata a noop.
  1166. */
  1167. err2 = nfserrno(commit_metadata(fhp));
  1168. if (err2)
  1169. err = err2;
  1170. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1171. /*
  1172. * Update the file handle to get the new inode info.
  1173. */
  1174. if (!err)
  1175. err = fh_update(resfhp);
  1176. out:
  1177. if (dchild && !IS_ERR(dchild))
  1178. dput(dchild);
  1179. return err;
  1180. out_nfserr:
  1181. err = nfserrno(host_err);
  1182. goto out;
  1183. }
  1184. #ifdef CONFIG_NFSD_V3
  1185. /*
  1186. * NFSv3 version of nfsd_create
  1187. */
  1188. __be32
  1189. nfsd_create_v3(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1190. char *fname, int flen, struct iattr *iap,
  1191. struct svc_fh *resfhp, int createmode, u32 *verifier,
  1192. int *truncp, int *created)
  1193. {
  1194. struct dentry *dentry, *dchild = NULL;
  1195. struct inode *dirp;
  1196. __be32 err;
  1197. int host_err;
  1198. __u32 v_mtime=0, v_atime=0;
  1199. err = nfserr_perm;
  1200. if (!flen)
  1201. goto out;
  1202. err = nfserr_exist;
  1203. if (isdotent(fname, flen))
  1204. goto out;
  1205. if (!(iap->ia_valid & ATTR_MODE))
  1206. iap->ia_mode = 0;
  1207. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1208. if (err)
  1209. goto out;
  1210. dentry = fhp->fh_dentry;
  1211. dirp = dentry->d_inode;
  1212. /* Get all the sanity checks out of the way before
  1213. * we lock the parent. */
  1214. err = nfserr_notdir;
  1215. if (!dirp->i_op->lookup)
  1216. goto out;
  1217. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1218. /*
  1219. * Compose the response file handle.
  1220. */
  1221. dchild = lookup_one_len(fname, dentry, flen);
  1222. host_err = PTR_ERR(dchild);
  1223. if (IS_ERR(dchild))
  1224. goto out_nfserr;
  1225. err = fh_compose(resfhp, fhp->fh_export, dchild, fhp);
  1226. if (err)
  1227. goto out;
  1228. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1229. /* solaris7 gets confused (bugid 4218508) if these have
  1230. * the high bit set, so just clear the high bits. If this is
  1231. * ever changed to use different attrs for storing the
  1232. * verifier, then do_open_lookup() will also need to be fixed
  1233. * accordingly.
  1234. */
  1235. v_mtime = verifier[0]&0x7fffffff;
  1236. v_atime = verifier[1]&0x7fffffff;
  1237. }
  1238. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1239. if (host_err)
  1240. goto out_nfserr;
  1241. if (dchild->d_inode) {
  1242. err = 0;
  1243. switch (createmode) {
  1244. case NFS3_CREATE_UNCHECKED:
  1245. if (! S_ISREG(dchild->d_inode->i_mode))
  1246. err = nfserr_exist;
  1247. else if (truncp) {
  1248. /* in nfsv4, we need to treat this case a little
  1249. * differently. we don't want to truncate the
  1250. * file now; this would be wrong if the OPEN
  1251. * fails for some other reason. furthermore,
  1252. * if the size is nonzero, we should ignore it
  1253. * according to spec!
  1254. */
  1255. *truncp = (iap->ia_valid & ATTR_SIZE) && !iap->ia_size;
  1256. }
  1257. else {
  1258. iap->ia_valid &= ATTR_SIZE;
  1259. goto set_attr;
  1260. }
  1261. break;
  1262. case NFS3_CREATE_EXCLUSIVE:
  1263. if ( dchild->d_inode->i_mtime.tv_sec == v_mtime
  1264. && dchild->d_inode->i_atime.tv_sec == v_atime
  1265. && dchild->d_inode->i_size == 0 )
  1266. break;
  1267. /* fallthru */
  1268. case NFS3_CREATE_GUARDED:
  1269. err = nfserr_exist;
  1270. }
  1271. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1272. goto out;
  1273. }
  1274. host_err = vfs_create(dirp, dchild, iap->ia_mode, NULL);
  1275. if (host_err < 0) {
  1276. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1277. goto out_nfserr;
  1278. }
  1279. if (created)
  1280. *created = 1;
  1281. nfsd_check_ignore_resizing(iap);
  1282. if (createmode == NFS3_CREATE_EXCLUSIVE) {
  1283. /* Cram the verifier into atime/mtime */
  1284. iap->ia_valid = ATTR_MTIME|ATTR_ATIME
  1285. | ATTR_MTIME_SET|ATTR_ATIME_SET;
  1286. /* XXX someone who knows this better please fix it for nsec */
  1287. iap->ia_mtime.tv_sec = v_mtime;
  1288. iap->ia_atime.tv_sec = v_atime;
  1289. iap->ia_mtime.tv_nsec = 0;
  1290. iap->ia_atime.tv_nsec = 0;
  1291. }
  1292. set_attr:
  1293. err = nfsd_create_setattr(rqstp, resfhp, iap);
  1294. /*
  1295. * nfsd_setattr already committed the child (and possibly also the parent).
  1296. */
  1297. if (!err)
  1298. err = nfserrno(commit_metadata(fhp));
  1299. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1300. /*
  1301. * Update the filehandle to get the new inode info.
  1302. */
  1303. if (!err)
  1304. err = fh_update(resfhp);
  1305. out:
  1306. fh_unlock(fhp);
  1307. if (dchild && !IS_ERR(dchild))
  1308. dput(dchild);
  1309. return err;
  1310. out_nfserr:
  1311. err = nfserrno(host_err);
  1312. goto out;
  1313. }
  1314. #endif /* CONFIG_NFSD_V3 */
  1315. /*
  1316. * Read a symlink. On entry, *lenp must contain the maximum path length that
  1317. * fits into the buffer. On return, it contains the true length.
  1318. * N.B. After this call fhp needs an fh_put
  1319. */
  1320. __be32
  1321. nfsd_readlink(struct svc_rqst *rqstp, struct svc_fh *fhp, char *buf, int *lenp)
  1322. {
  1323. struct dentry *dentry;
  1324. struct inode *inode;
  1325. mm_segment_t oldfs;
  1326. __be32 err;
  1327. int host_err;
  1328. err = fh_verify(rqstp, fhp, S_IFLNK, NFSD_MAY_NOP);
  1329. if (err)
  1330. goto out;
  1331. dentry = fhp->fh_dentry;
  1332. inode = dentry->d_inode;
  1333. err = nfserr_inval;
  1334. if (!inode->i_op->readlink)
  1335. goto out;
  1336. touch_atime(fhp->fh_export->ex_path.mnt, dentry);
  1337. /* N.B. Why does this call need a get_fs()??
  1338. * Remove the set_fs and watch the fireworks:-) --okir
  1339. */
  1340. oldfs = get_fs(); set_fs(KERNEL_DS);
  1341. host_err = inode->i_op->readlink(dentry, buf, *lenp);
  1342. set_fs(oldfs);
  1343. if (host_err < 0)
  1344. goto out_nfserr;
  1345. *lenp = host_err;
  1346. err = 0;
  1347. out:
  1348. return err;
  1349. out_nfserr:
  1350. err = nfserrno(host_err);
  1351. goto out;
  1352. }
  1353. /*
  1354. * Create a symlink and look up its inode
  1355. * N.B. After this call _both_ fhp and resfhp need an fh_put
  1356. */
  1357. __be32
  1358. nfsd_symlink(struct svc_rqst *rqstp, struct svc_fh *fhp,
  1359. char *fname, int flen,
  1360. char *path, int plen,
  1361. struct svc_fh *resfhp,
  1362. struct iattr *iap)
  1363. {
  1364. struct dentry *dentry, *dnew;
  1365. __be32 err, cerr;
  1366. int host_err;
  1367. err = nfserr_noent;
  1368. if (!flen || !plen)
  1369. goto out;
  1370. err = nfserr_exist;
  1371. if (isdotent(fname, flen))
  1372. goto out;
  1373. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_CREATE);
  1374. if (err)
  1375. goto out;
  1376. fh_lock(fhp);
  1377. dentry = fhp->fh_dentry;
  1378. dnew = lookup_one_len(fname, dentry, flen);
  1379. host_err = PTR_ERR(dnew);
  1380. if (IS_ERR(dnew))
  1381. goto out_nfserr;
  1382. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1383. if (host_err)
  1384. goto out_nfserr;
  1385. if (unlikely(path[plen] != 0)) {
  1386. char *path_alloced = kmalloc(plen+1, GFP_KERNEL);
  1387. if (path_alloced == NULL)
  1388. host_err = -ENOMEM;
  1389. else {
  1390. strncpy(path_alloced, path, plen);
  1391. path_alloced[plen] = 0;
  1392. host_err = vfs_symlink(dentry->d_inode, dnew, path_alloced);
  1393. kfree(path_alloced);
  1394. }
  1395. } else
  1396. host_err = vfs_symlink(dentry->d_inode, dnew, path);
  1397. err = nfserrno(host_err);
  1398. if (!err)
  1399. err = nfserrno(commit_metadata(fhp));
  1400. fh_unlock(fhp);
  1401. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1402. cerr = fh_compose(resfhp, fhp->fh_export, dnew, fhp);
  1403. dput(dnew);
  1404. if (err==0) err = cerr;
  1405. out:
  1406. return err;
  1407. out_nfserr:
  1408. err = nfserrno(host_err);
  1409. goto out;
  1410. }
  1411. /*
  1412. * Create a hardlink
  1413. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1414. */
  1415. __be32
  1416. nfsd_link(struct svc_rqst *rqstp, struct svc_fh *ffhp,
  1417. char *name, int len, struct svc_fh *tfhp)
  1418. {
  1419. struct dentry *ddir, *dnew, *dold;
  1420. struct inode *dirp;
  1421. __be32 err;
  1422. int host_err;
  1423. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_CREATE);
  1424. if (err)
  1425. goto out;
  1426. err = fh_verify(rqstp, tfhp, -S_IFDIR, NFSD_MAY_NOP);
  1427. if (err)
  1428. goto out;
  1429. err = nfserr_perm;
  1430. if (!len)
  1431. goto out;
  1432. err = nfserr_exist;
  1433. if (isdotent(name, len))
  1434. goto out;
  1435. fh_lock_nested(ffhp, I_MUTEX_PARENT);
  1436. ddir = ffhp->fh_dentry;
  1437. dirp = ddir->d_inode;
  1438. dnew = lookup_one_len(name, ddir, len);
  1439. host_err = PTR_ERR(dnew);
  1440. if (IS_ERR(dnew))
  1441. goto out_nfserr;
  1442. dold = tfhp->fh_dentry;
  1443. host_err = mnt_want_write(tfhp->fh_export->ex_path.mnt);
  1444. if (host_err) {
  1445. err = nfserrno(host_err);
  1446. goto out_dput;
  1447. }
  1448. err = nfserr_noent;
  1449. if (!dold->d_inode)
  1450. goto out_drop_write;
  1451. host_err = nfsd_break_lease(dold->d_inode);
  1452. if (host_err)
  1453. goto out_drop_write;
  1454. host_err = vfs_link(dold, dirp, dnew);
  1455. if (!host_err) {
  1456. err = nfserrno(commit_metadata(ffhp));
  1457. if (!err)
  1458. err = nfserrno(commit_metadata(tfhp));
  1459. } else {
  1460. if (host_err == -EXDEV && rqstp->rq_vers == 2)
  1461. err = nfserr_acces;
  1462. else
  1463. err = nfserrno(host_err);
  1464. }
  1465. out_drop_write:
  1466. mnt_drop_write(tfhp->fh_export->ex_path.mnt);
  1467. out_dput:
  1468. dput(dnew);
  1469. out_unlock:
  1470. fh_unlock(ffhp);
  1471. out:
  1472. return err;
  1473. out_nfserr:
  1474. err = nfserrno(host_err);
  1475. goto out_unlock;
  1476. }
  1477. /*
  1478. * Rename a file
  1479. * N.B. After this call _both_ ffhp and tfhp need an fh_put
  1480. */
  1481. __be32
  1482. nfsd_rename(struct svc_rqst *rqstp, struct svc_fh *ffhp, char *fname, int flen,
  1483. struct svc_fh *tfhp, char *tname, int tlen)
  1484. {
  1485. struct dentry *fdentry, *tdentry, *odentry, *ndentry, *trap;
  1486. struct inode *fdir, *tdir;
  1487. __be32 err;
  1488. int host_err;
  1489. err = fh_verify(rqstp, ffhp, S_IFDIR, NFSD_MAY_REMOVE);
  1490. if (err)
  1491. goto out;
  1492. err = fh_verify(rqstp, tfhp, S_IFDIR, NFSD_MAY_CREATE);
  1493. if (err)
  1494. goto out;
  1495. fdentry = ffhp->fh_dentry;
  1496. fdir = fdentry->d_inode;
  1497. tdentry = tfhp->fh_dentry;
  1498. tdir = tdentry->d_inode;
  1499. err = (rqstp->rq_vers == 2) ? nfserr_acces : nfserr_xdev;
  1500. if (ffhp->fh_export != tfhp->fh_export)
  1501. goto out;
  1502. err = nfserr_perm;
  1503. if (!flen || isdotent(fname, flen) || !tlen || isdotent(tname, tlen))
  1504. goto out;
  1505. /* cannot use fh_lock as we need deadlock protective ordering
  1506. * so do it by hand */
  1507. trap = lock_rename(tdentry, fdentry);
  1508. ffhp->fh_locked = tfhp->fh_locked = 1;
  1509. fill_pre_wcc(ffhp);
  1510. fill_pre_wcc(tfhp);
  1511. odentry = lookup_one_len(fname, fdentry, flen);
  1512. host_err = PTR_ERR(odentry);
  1513. if (IS_ERR(odentry))
  1514. goto out_nfserr;
  1515. host_err = -ENOENT;
  1516. if (!odentry->d_inode)
  1517. goto out_dput_old;
  1518. host_err = -EINVAL;
  1519. if (odentry == trap)
  1520. goto out_dput_old;
  1521. ndentry = lookup_one_len(tname, tdentry, tlen);
  1522. host_err = PTR_ERR(ndentry);
  1523. if (IS_ERR(ndentry))
  1524. goto out_dput_old;
  1525. host_err = -ENOTEMPTY;
  1526. if (ndentry == trap)
  1527. goto out_dput_new;
  1528. host_err = -EXDEV;
  1529. if (ffhp->fh_export->ex_path.mnt != tfhp->fh_export->ex_path.mnt)
  1530. goto out_dput_new;
  1531. host_err = mnt_want_write(ffhp->fh_export->ex_path.mnt);
  1532. if (host_err)
  1533. goto out_dput_new;
  1534. host_err = nfsd_break_lease(odentry->d_inode);
  1535. if (host_err)
  1536. goto out_drop_write;
  1537. if (ndentry->d_inode) {
  1538. host_err = nfsd_break_lease(ndentry->d_inode);
  1539. if (host_err)
  1540. goto out_drop_write;
  1541. }
  1542. if (host_err)
  1543. goto out_drop_write;
  1544. host_err = vfs_rename(fdir, odentry, tdir, ndentry);
  1545. if (!host_err) {
  1546. host_err = commit_metadata(tfhp);
  1547. if (!host_err)
  1548. host_err = commit_metadata(ffhp);
  1549. }
  1550. out_drop_write:
  1551. mnt_drop_write(ffhp->fh_export->ex_path.mnt);
  1552. out_dput_new:
  1553. dput(ndentry);
  1554. out_dput_old:
  1555. dput(odentry);
  1556. out_nfserr:
  1557. err = nfserrno(host_err);
  1558. /* we cannot reply on fh_unlock on the two filehandles,
  1559. * as that would do the wrong thing if the two directories
  1560. * were the same, so again we do it by hand
  1561. */
  1562. fill_post_wcc(ffhp);
  1563. fill_post_wcc(tfhp);
  1564. unlock_rename(tdentry, fdentry);
  1565. ffhp->fh_locked = tfhp->fh_locked = 0;
  1566. out:
  1567. return err;
  1568. }
  1569. /*
  1570. * Unlink a file or directory
  1571. * N.B. After this call fhp needs an fh_put
  1572. */
  1573. __be32
  1574. nfsd_unlink(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
  1575. char *fname, int flen)
  1576. {
  1577. struct dentry *dentry, *rdentry;
  1578. struct inode *dirp;
  1579. __be32 err;
  1580. int host_err;
  1581. err = nfserr_acces;
  1582. if (!flen || isdotent(fname, flen))
  1583. goto out;
  1584. err = fh_verify(rqstp, fhp, S_IFDIR, NFSD_MAY_REMOVE);
  1585. if (err)
  1586. goto out;
  1587. fh_lock_nested(fhp, I_MUTEX_PARENT);
  1588. dentry = fhp->fh_dentry;
  1589. dirp = dentry->d_inode;
  1590. rdentry = lookup_one_len(fname, dentry, flen);
  1591. host_err = PTR_ERR(rdentry);
  1592. if (IS_ERR(rdentry))
  1593. goto out_nfserr;
  1594. if (!rdentry->d_inode) {
  1595. dput(rdentry);
  1596. err = nfserr_noent;
  1597. goto out;
  1598. }
  1599. if (!type)
  1600. type = rdentry->d_inode->i_mode & S_IFMT;
  1601. host_err = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1602. if (host_err)
  1603. goto out_put;
  1604. host_err = nfsd_break_lease(rdentry->d_inode);
  1605. if (host_err)
  1606. goto out_drop_write;
  1607. if (type != S_IFDIR)
  1608. host_err = vfs_unlink(dirp, rdentry);
  1609. else
  1610. host_err = vfs_rmdir(dirp, rdentry);
  1611. if (!host_err)
  1612. host_err = commit_metadata(fhp);
  1613. out_drop_write:
  1614. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1615. out_put:
  1616. dput(rdentry);
  1617. out_nfserr:
  1618. err = nfserrno(host_err);
  1619. out:
  1620. return err;
  1621. }
  1622. /*
  1623. * We do this buffering because we must not call back into the file
  1624. * system's ->lookup() method from the filldir callback. That may well
  1625. * deadlock a number of file systems.
  1626. *
  1627. * This is based heavily on the implementation of same in XFS.
  1628. */
  1629. struct buffered_dirent {
  1630. u64 ino;
  1631. loff_t offset;
  1632. int namlen;
  1633. unsigned int d_type;
  1634. char name[];
  1635. };
  1636. struct readdir_data {
  1637. char *dirent;
  1638. size_t used;
  1639. int full;
  1640. };
  1641. static int nfsd_buffered_filldir(void *__buf, const char *name, int namlen,
  1642. loff_t offset, u64 ino, unsigned int d_type)
  1643. {
  1644. struct readdir_data *buf = __buf;
  1645. struct buffered_dirent *de = (void *)(buf->dirent + buf->used);
  1646. unsigned int reclen;
  1647. reclen = ALIGN(sizeof(struct buffered_dirent) + namlen, sizeof(u64));
  1648. if (buf->used + reclen > PAGE_SIZE) {
  1649. buf->full = 1;
  1650. return -EINVAL;
  1651. }
  1652. de->namlen = namlen;
  1653. de->offset = offset;
  1654. de->ino = ino;
  1655. de->d_type = d_type;
  1656. memcpy(de->name, name, namlen);
  1657. buf->used += reclen;
  1658. return 0;
  1659. }
  1660. static __be32 nfsd_buffered_readdir(struct file *file, filldir_t func,
  1661. struct readdir_cd *cdp, loff_t *offsetp)
  1662. {
  1663. struct readdir_data buf;
  1664. struct buffered_dirent *de;
  1665. int host_err;
  1666. int size;
  1667. loff_t offset;
  1668. buf.dirent = (void *)__get_free_page(GFP_KERNEL);
  1669. if (!buf.dirent)
  1670. return nfserrno(-ENOMEM);
  1671. offset = *offsetp;
  1672. while (1) {
  1673. struct inode *dir_inode = file->f_path.dentry->d_inode;
  1674. unsigned int reclen;
  1675. cdp->err = nfserr_eof; /* will be cleared on successful read */
  1676. buf.used = 0;
  1677. buf.full = 0;
  1678. host_err = vfs_readdir(file, nfsd_buffered_filldir, &buf);
  1679. if (buf.full)
  1680. host_err = 0;
  1681. if (host_err < 0)
  1682. break;
  1683. size = buf.used;
  1684. if (!size)
  1685. break;
  1686. /*
  1687. * Various filldir functions may end up calling back into
  1688. * lookup_one_len() and the file system's ->lookup() method.
  1689. * These expect i_mutex to be held, as it would within readdir.
  1690. */
  1691. host_err = mutex_lock_killable(&dir_inode->i_mutex);
  1692. if (host_err)
  1693. break;
  1694. de = (struct buffered_dirent *)buf.dirent;
  1695. while (size > 0) {
  1696. offset = de->offset;
  1697. if (func(cdp, de->name, de->namlen, de->offset,
  1698. de->ino, de->d_type))
  1699. break;
  1700. if (cdp->err != nfs_ok)
  1701. break;
  1702. reclen = ALIGN(sizeof(*de) + de->namlen,
  1703. sizeof(u64));
  1704. size -= reclen;
  1705. de = (struct buffered_dirent *)((char *)de + reclen);
  1706. }
  1707. mutex_unlock(&dir_inode->i_mutex);
  1708. if (size > 0) /* We bailed out early */
  1709. break;
  1710. offset = vfs_llseek(file, 0, SEEK_CUR);
  1711. }
  1712. free_page((unsigned long)(buf.dirent));
  1713. if (host_err)
  1714. return nfserrno(host_err);
  1715. *offsetp = offset;
  1716. return cdp->err;
  1717. }
  1718. /*
  1719. * Read entries from a directory.
  1720. * The NFSv3/4 verifier we ignore for now.
  1721. */
  1722. __be32
  1723. nfsd_readdir(struct svc_rqst *rqstp, struct svc_fh *fhp, loff_t *offsetp,
  1724. struct readdir_cd *cdp, filldir_t func)
  1725. {
  1726. __be32 err;
  1727. struct file *file;
  1728. loff_t offset = *offsetp;
  1729. err = nfsd_open(rqstp, fhp, S_IFDIR, NFSD_MAY_READ, &file);
  1730. if (err)
  1731. goto out;
  1732. offset = vfs_llseek(file, offset, 0);
  1733. if (offset < 0) {
  1734. err = nfserrno((int)offset);
  1735. goto out_close;
  1736. }
  1737. err = nfsd_buffered_readdir(file, func, cdp, offsetp);
  1738. if (err == nfserr_eof || err == nfserr_toosmall)
  1739. err = nfs_ok; /* can still be found in ->err */
  1740. out_close:
  1741. nfsd_close(file);
  1742. out:
  1743. return err;
  1744. }
  1745. /*
  1746. * Get file system stats
  1747. * N.B. After this call fhp needs an fh_put
  1748. */
  1749. __be32
  1750. nfsd_statfs(struct svc_rqst *rqstp, struct svc_fh *fhp, struct kstatfs *stat, int access)
  1751. {
  1752. __be32 err;
  1753. err = fh_verify(rqstp, fhp, 0, NFSD_MAY_NOP | access);
  1754. if (!err) {
  1755. struct path path = {
  1756. .mnt = fhp->fh_export->ex_path.mnt,
  1757. .dentry = fhp->fh_dentry,
  1758. };
  1759. if (vfs_statfs(&path, stat))
  1760. err = nfserr_io;
  1761. }
  1762. return err;
  1763. }
  1764. static int exp_rdonly(struct svc_rqst *rqstp, struct svc_export *exp)
  1765. {
  1766. return nfsexp_flags(rqstp, exp) & NFSEXP_READONLY;
  1767. }
  1768. /*
  1769. * Check for a user's access permissions to this inode.
  1770. */
  1771. __be32
  1772. nfsd_permission(struct svc_rqst *rqstp, struct svc_export *exp,
  1773. struct dentry *dentry, int acc)
  1774. {
  1775. struct inode *inode = dentry->d_inode;
  1776. int err;
  1777. if (acc == NFSD_MAY_NOP)
  1778. return 0;
  1779. #if 0
  1780. dprintk("nfsd: permission 0x%x%s%s%s%s%s%s%s mode 0%o%s%s%s\n",
  1781. acc,
  1782. (acc & NFSD_MAY_READ)? " read" : "",
  1783. (acc & NFSD_MAY_WRITE)? " write" : "",
  1784. (acc & NFSD_MAY_EXEC)? " exec" : "",
  1785. (acc & NFSD_MAY_SATTR)? " sattr" : "",
  1786. (acc & NFSD_MAY_TRUNC)? " trunc" : "",
  1787. (acc & NFSD_MAY_LOCK)? " lock" : "",
  1788. (acc & NFSD_MAY_OWNER_OVERRIDE)? " owneroverride" : "",
  1789. inode->i_mode,
  1790. IS_IMMUTABLE(inode)? " immut" : "",
  1791. IS_APPEND(inode)? " append" : "",
  1792. __mnt_is_readonly(exp->ex_path.mnt)? " ro" : "");
  1793. dprintk(" owner %d/%d user %d/%d\n",
  1794. inode->i_uid, inode->i_gid, current_fsuid(), current_fsgid());
  1795. #endif
  1796. /* Normally we reject any write/sattr etc access on a read-only file
  1797. * system. But if it is IRIX doing check on write-access for a
  1798. * device special file, we ignore rofs.
  1799. */
  1800. if (!(acc & NFSD_MAY_LOCAL_ACCESS))
  1801. if (acc & (NFSD_MAY_WRITE | NFSD_MAY_SATTR | NFSD_MAY_TRUNC)) {
  1802. if (exp_rdonly(rqstp, exp) ||
  1803. __mnt_is_readonly(exp->ex_path.mnt))
  1804. return nfserr_rofs;
  1805. if (/* (acc & NFSD_MAY_WRITE) && */ IS_IMMUTABLE(inode))
  1806. return nfserr_perm;
  1807. }
  1808. if ((acc & NFSD_MAY_TRUNC) && IS_APPEND(inode))
  1809. return nfserr_perm;
  1810. if (acc & NFSD_MAY_LOCK) {
  1811. /* If we cannot rely on authentication in NLM requests,
  1812. * just allow locks, otherwise require read permission, or
  1813. * ownership
  1814. */
  1815. if (exp->ex_flags & NFSEXP_NOAUTHNLM)
  1816. return 0;
  1817. else
  1818. acc = NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE;
  1819. }
  1820. /*
  1821. * The file owner always gets access permission for accesses that
  1822. * would normally be checked at open time. This is to make
  1823. * file access work even when the client has done a fchmod(fd, 0).
  1824. *
  1825. * However, `cp foo bar' should fail nevertheless when bar is
  1826. * readonly. A sensible way to do this might be to reject all
  1827. * attempts to truncate a read-only file, because a creat() call
  1828. * always implies file truncation.
  1829. * ... but this isn't really fair. A process may reasonably call
  1830. * ftruncate on an open file descriptor on a file with perm 000.
  1831. * We must trust the client to do permission checking - using "ACCESS"
  1832. * with NFSv3.
  1833. */
  1834. if ((acc & NFSD_MAY_OWNER_OVERRIDE) &&
  1835. inode->i_uid == current_fsuid())
  1836. return 0;
  1837. /* This assumes NFSD_MAY_{READ,WRITE,EXEC} == MAY_{READ,WRITE,EXEC} */
  1838. err = inode_permission(inode, acc & (MAY_READ|MAY_WRITE|MAY_EXEC));
  1839. /* Allow read access to binaries even when mode 111 */
  1840. if (err == -EACCES && S_ISREG(inode->i_mode) &&
  1841. acc == (NFSD_MAY_READ | NFSD_MAY_OWNER_OVERRIDE))
  1842. err = inode_permission(inode, MAY_EXEC);
  1843. return err? nfserrno(err) : 0;
  1844. }
  1845. void
  1846. nfsd_racache_shutdown(void)
  1847. {
  1848. struct raparms *raparm, *last_raparm;
  1849. unsigned int i;
  1850. dprintk("nfsd: freeing readahead buffers.\n");
  1851. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1852. raparm = raparm_hash[i].pb_head;
  1853. while(raparm) {
  1854. last_raparm = raparm;
  1855. raparm = raparm->p_next;
  1856. kfree(last_raparm);
  1857. }
  1858. raparm_hash[i].pb_head = NULL;
  1859. }
  1860. }
  1861. /*
  1862. * Initialize readahead param cache
  1863. */
  1864. int
  1865. nfsd_racache_init(int cache_size)
  1866. {
  1867. int i;
  1868. int j = 0;
  1869. int nperbucket;
  1870. struct raparms **raparm = NULL;
  1871. if (raparm_hash[0].pb_head)
  1872. return 0;
  1873. nperbucket = DIV_ROUND_UP(cache_size, RAPARM_HASH_SIZE);
  1874. if (nperbucket < 2)
  1875. nperbucket = 2;
  1876. cache_size = nperbucket * RAPARM_HASH_SIZE;
  1877. dprintk("nfsd: allocating %d readahead buffers.\n", cache_size);
  1878. for (i = 0; i < RAPARM_HASH_SIZE; i++) {
  1879. spin_lock_init(&raparm_hash[i].pb_lock);
  1880. raparm = &raparm_hash[i].pb_head;
  1881. for (j = 0; j < nperbucket; j++) {
  1882. *raparm = kzalloc(sizeof(struct raparms), GFP_KERNEL);
  1883. if (!*raparm)
  1884. goto out_nomem;
  1885. raparm = &(*raparm)->p_next;
  1886. }
  1887. *raparm = NULL;
  1888. }
  1889. nfsdstats.ra_size = cache_size;
  1890. return 0;
  1891. out_nomem:
  1892. dprintk("nfsd: kmalloc failed, freeing readahead buffers\n");
  1893. nfsd_racache_shutdown();
  1894. return -ENOMEM;
  1895. }
  1896. #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
  1897. struct posix_acl *
  1898. nfsd_get_posix_acl(struct svc_fh *fhp, int type)
  1899. {
  1900. struct inode *inode = fhp->fh_dentry->d_inode;
  1901. char *name;
  1902. void *value = NULL;
  1903. ssize_t size;
  1904. struct posix_acl *acl;
  1905. if (!IS_POSIXACL(inode))
  1906. return ERR_PTR(-EOPNOTSUPP);
  1907. switch (type) {
  1908. case ACL_TYPE_ACCESS:
  1909. name = POSIX_ACL_XATTR_ACCESS;
  1910. break;
  1911. case ACL_TYPE_DEFAULT:
  1912. name = POSIX_ACL_XATTR_DEFAULT;
  1913. break;
  1914. default:
  1915. return ERR_PTR(-EOPNOTSUPP);
  1916. }
  1917. size = nfsd_getxattr(fhp->fh_dentry, name, &value);
  1918. if (size < 0)
  1919. return ERR_PTR(size);
  1920. acl = posix_acl_from_xattr(value, size);
  1921. kfree(value);
  1922. return acl;
  1923. }
  1924. int
  1925. nfsd_set_posix_acl(struct svc_fh *fhp, int type, struct posix_acl *acl)
  1926. {
  1927. struct inode *inode = fhp->fh_dentry->d_inode;
  1928. char *name;
  1929. void *value = NULL;
  1930. size_t size;
  1931. int error;
  1932. if (!IS_POSIXACL(inode) ||
  1933. !inode->i_op->setxattr || !inode->i_op->removexattr)
  1934. return -EOPNOTSUPP;
  1935. switch(type) {
  1936. case ACL_TYPE_ACCESS:
  1937. name = POSIX_ACL_XATTR_ACCESS;
  1938. break;
  1939. case ACL_TYPE_DEFAULT:
  1940. name = POSIX_ACL_XATTR_DEFAULT;
  1941. break;
  1942. default:
  1943. return -EOPNOTSUPP;
  1944. }
  1945. if (acl && acl->a_count) {
  1946. size = posix_acl_xattr_size(acl->a_count);
  1947. value = kmalloc(size, GFP_KERNEL);
  1948. if (!value)
  1949. return -ENOMEM;
  1950. error = posix_acl_to_xattr(acl, value, size);
  1951. if (error < 0)
  1952. goto getout;
  1953. size = error;
  1954. } else
  1955. size = 0;
  1956. error = mnt_want_write(fhp->fh_export->ex_path.mnt);
  1957. if (error)
  1958. goto getout;
  1959. if (size)
  1960. error = vfs_setxattr(fhp->fh_dentry, name, value, size, 0);
  1961. else {
  1962. if (!S_ISDIR(inode->i_mode) && type == ACL_TYPE_DEFAULT)
  1963. error = 0;
  1964. else {
  1965. error = vfs_removexattr(fhp->fh_dentry, name);
  1966. if (error == -ENODATA)
  1967. error = 0;
  1968. }
  1969. }
  1970. mnt_drop_write(fhp->fh_export->ex_path.mnt);
  1971. getout:
  1972. kfree(value);
  1973. return error;
  1974. }
  1975. #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */