vfs.c 53 KB

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