vfs.c 52 KB

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