vfs.c 54 KB

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