vfs.c 51 KB

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