vfs.c 54 KB

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