vfs.c 55 KB

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