vfs.c 55 KB

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