vfs.c 54 KB

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