vfs.c 55 KB

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