vfs.c 54 KB

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