vfs.c 53 KB

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