vfs.c 53 KB

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