vfs.c 49 KB

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