dir.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007
  1. /*
  2. * linux/fs/nfs/dir.c
  3. *
  4. * Copyright (C) 1992 Rick Sladkey
  5. *
  6. * nfs directory handling functions
  7. *
  8. * 10 Apr 1996 Added silly rename for unlink --okir
  9. * 28 Sep 1996 Improved directory cache --okir
  10. * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de
  11. * Re-implemented silly rename for unlink, newly implemented
  12. * silly rename for nfs_rename() following the suggestions
  13. * of Olaf Kirch (okir) found in this file.
  14. * Following Linus comments on my original hack, this version
  15. * depends only on the dcache stuff and doesn't touch the inode
  16. * layer (iput() and friends).
  17. * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM
  18. */
  19. #include <linux/time.h>
  20. #include <linux/errno.h>
  21. #include <linux/stat.h>
  22. #include <linux/fcntl.h>
  23. #include <linux/string.h>
  24. #include <linux/kernel.h>
  25. #include <linux/slab.h>
  26. #include <linux/mm.h>
  27. #include <linux/sunrpc/clnt.h>
  28. #include <linux/nfs_fs.h>
  29. #include <linux/nfs_mount.h>
  30. #include <linux/pagemap.h>
  31. #include <linux/pagevec.h>
  32. #include <linux/namei.h>
  33. #include <linux/mount.h>
  34. #include <linux/sched.h>
  35. #include "nfs4_fs.h"
  36. #include "delegation.h"
  37. #include "iostat.h"
  38. #include "internal.h"
  39. /* #define NFS_DEBUG_VERBOSE 1 */
  40. static int nfs_opendir(struct inode *, struct file *);
  41. static int nfs_readdir(struct file *, void *, filldir_t);
  42. static struct dentry *nfs_lookup(struct inode *, struct dentry *, struct nameidata *);
  43. static int nfs_create(struct inode *, struct dentry *, int, struct nameidata *);
  44. static int nfs_mkdir(struct inode *, struct dentry *, int);
  45. static int nfs_rmdir(struct inode *, struct dentry *);
  46. static int nfs_unlink(struct inode *, struct dentry *);
  47. static int nfs_symlink(struct inode *, struct dentry *, const char *);
  48. static int nfs_link(struct dentry *, struct inode *, struct dentry *);
  49. static int nfs_mknod(struct inode *, struct dentry *, int, dev_t);
  50. static int nfs_rename(struct inode *, struct dentry *,
  51. struct inode *, struct dentry *);
  52. static int nfs_fsync_dir(struct file *, int);
  53. static loff_t nfs_llseek_dir(struct file *, loff_t, int);
  54. const struct file_operations nfs_dir_operations = {
  55. .llseek = nfs_llseek_dir,
  56. .read = generic_read_dir,
  57. .readdir = nfs_readdir,
  58. .open = nfs_opendir,
  59. .release = nfs_release,
  60. .fsync = nfs_fsync_dir,
  61. };
  62. const struct inode_operations nfs_dir_inode_operations = {
  63. .create = nfs_create,
  64. .lookup = nfs_lookup,
  65. .link = nfs_link,
  66. .unlink = nfs_unlink,
  67. .symlink = nfs_symlink,
  68. .mkdir = nfs_mkdir,
  69. .rmdir = nfs_rmdir,
  70. .mknod = nfs_mknod,
  71. .rename = nfs_rename,
  72. .permission = nfs_permission,
  73. .getattr = nfs_getattr,
  74. .setattr = nfs_setattr,
  75. };
  76. #ifdef CONFIG_NFS_V3
  77. const struct inode_operations nfs3_dir_inode_operations = {
  78. .create = nfs_create,
  79. .lookup = nfs_lookup,
  80. .link = nfs_link,
  81. .unlink = nfs_unlink,
  82. .symlink = nfs_symlink,
  83. .mkdir = nfs_mkdir,
  84. .rmdir = nfs_rmdir,
  85. .mknod = nfs_mknod,
  86. .rename = nfs_rename,
  87. .permission = nfs_permission,
  88. .getattr = nfs_getattr,
  89. .setattr = nfs_setattr,
  90. .listxattr = nfs3_listxattr,
  91. .getxattr = nfs3_getxattr,
  92. .setxattr = nfs3_setxattr,
  93. .removexattr = nfs3_removexattr,
  94. };
  95. #endif /* CONFIG_NFS_V3 */
  96. #ifdef CONFIG_NFS_V4
  97. static struct dentry *nfs_atomic_lookup(struct inode *, struct dentry *, struct nameidata *);
  98. const struct inode_operations nfs4_dir_inode_operations = {
  99. .create = nfs_create,
  100. .lookup = nfs_atomic_lookup,
  101. .link = nfs_link,
  102. .unlink = nfs_unlink,
  103. .symlink = nfs_symlink,
  104. .mkdir = nfs_mkdir,
  105. .rmdir = nfs_rmdir,
  106. .mknod = nfs_mknod,
  107. .rename = nfs_rename,
  108. .permission = nfs_permission,
  109. .getattr = nfs_getattr,
  110. .setattr = nfs_setattr,
  111. .getxattr = nfs4_getxattr,
  112. .setxattr = nfs4_setxattr,
  113. .listxattr = nfs4_listxattr,
  114. };
  115. #endif /* CONFIG_NFS_V4 */
  116. /*
  117. * Open file
  118. */
  119. static int
  120. nfs_opendir(struct inode *inode, struct file *filp)
  121. {
  122. int res;
  123. dfprintk(FILE, "NFS: open dir(%s/%s)\n",
  124. filp->f_path.dentry->d_parent->d_name.name,
  125. filp->f_path.dentry->d_name.name);
  126. nfs_inc_stats(inode, NFSIOS_VFSOPEN);
  127. /* Call generic open code in order to cache credentials */
  128. res = nfs_open(inode, filp);
  129. return res;
  130. }
  131. typedef __be32 * (*decode_dirent_t)(__be32 *, struct nfs_entry *, int);
  132. typedef struct {
  133. struct file *file;
  134. struct page *page;
  135. unsigned long page_index;
  136. __be32 *ptr;
  137. u64 *dir_cookie;
  138. loff_t current_index;
  139. struct nfs_entry *entry;
  140. decode_dirent_t decode;
  141. int plus;
  142. unsigned long timestamp;
  143. unsigned long gencount;
  144. int timestamp_valid;
  145. } nfs_readdir_descriptor_t;
  146. /* Now we cache directories properly, by stuffing the dirent
  147. * data directly in the page cache.
  148. *
  149. * Inode invalidation due to refresh etc. takes care of
  150. * _everything_, no sloppy entry flushing logic, no extraneous
  151. * copying, network direct to page cache, the way it was meant
  152. * to be.
  153. *
  154. * NOTE: Dirent information verification is done always by the
  155. * page-in of the RPC reply, nowhere else, this simplies
  156. * things substantially.
  157. */
  158. static
  159. int nfs_readdir_filler(nfs_readdir_descriptor_t *desc, struct page *page)
  160. {
  161. struct file *file = desc->file;
  162. struct inode *inode = file->f_path.dentry->d_inode;
  163. struct rpc_cred *cred = nfs_file_cred(file);
  164. unsigned long timestamp, gencount;
  165. int error;
  166. dfprintk(DIRCACHE, "NFS: %s: reading cookie %Lu into page %lu\n",
  167. __func__, (long long)desc->entry->cookie,
  168. page->index);
  169. again:
  170. timestamp = jiffies;
  171. gencount = nfs_inc_attr_generation_counter();
  172. error = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred, desc->entry->cookie, page,
  173. NFS_SERVER(inode)->dtsize, desc->plus);
  174. if (error < 0) {
  175. /* We requested READDIRPLUS, but the server doesn't grok it */
  176. if (error == -ENOTSUPP && desc->plus) {
  177. NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS;
  178. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  179. desc->plus = 0;
  180. goto again;
  181. }
  182. goto error;
  183. }
  184. desc->timestamp = timestamp;
  185. desc->gencount = gencount;
  186. desc->timestamp_valid = 1;
  187. SetPageUptodate(page);
  188. /* Ensure consistent page alignment of the data.
  189. * Note: assumes we have exclusive access to this mapping either
  190. * through inode->i_mutex or some other mechanism.
  191. */
  192. if (invalidate_inode_pages2_range(inode->i_mapping, page->index + 1, -1) < 0) {
  193. /* Should never happen */
  194. nfs_zap_mapping(inode, inode->i_mapping);
  195. }
  196. unlock_page(page);
  197. return 0;
  198. error:
  199. unlock_page(page);
  200. return -EIO;
  201. }
  202. static inline
  203. int dir_decode(nfs_readdir_descriptor_t *desc)
  204. {
  205. __be32 *p = desc->ptr;
  206. p = desc->decode(p, desc->entry, desc->plus);
  207. if (IS_ERR(p))
  208. return PTR_ERR(p);
  209. desc->ptr = p;
  210. if (desc->timestamp_valid) {
  211. desc->entry->fattr->time_start = desc->timestamp;
  212. desc->entry->fattr->gencount = desc->gencount;
  213. } else
  214. desc->entry->fattr->valid &= ~NFS_ATTR_FATTR;
  215. return 0;
  216. }
  217. static inline
  218. void dir_page_release(nfs_readdir_descriptor_t *desc)
  219. {
  220. kunmap(desc->page);
  221. page_cache_release(desc->page);
  222. desc->page = NULL;
  223. desc->ptr = NULL;
  224. }
  225. /*
  226. * Given a pointer to a buffer that has already been filled by a call
  227. * to readdir, find the next entry with cookie '*desc->dir_cookie'.
  228. *
  229. * If the end of the buffer has been reached, return -EAGAIN, if not,
  230. * return the offset within the buffer of the next entry to be
  231. * read.
  232. */
  233. static inline
  234. int find_dirent(nfs_readdir_descriptor_t *desc)
  235. {
  236. struct nfs_entry *entry = desc->entry;
  237. int loop_count = 0,
  238. status;
  239. while((status = dir_decode(desc)) == 0) {
  240. dfprintk(DIRCACHE, "NFS: %s: examining cookie %Lu\n",
  241. __func__, (unsigned long long)entry->cookie);
  242. if (entry->prev_cookie == *desc->dir_cookie)
  243. break;
  244. if (loop_count++ > 200) {
  245. loop_count = 0;
  246. schedule();
  247. }
  248. }
  249. return status;
  250. }
  251. /*
  252. * Given a pointer to a buffer that has already been filled by a call
  253. * to readdir, find the entry at offset 'desc->file->f_pos'.
  254. *
  255. * If the end of the buffer has been reached, return -EAGAIN, if not,
  256. * return the offset within the buffer of the next entry to be
  257. * read.
  258. */
  259. static inline
  260. int find_dirent_index(nfs_readdir_descriptor_t *desc)
  261. {
  262. struct nfs_entry *entry = desc->entry;
  263. int loop_count = 0,
  264. status;
  265. for(;;) {
  266. status = dir_decode(desc);
  267. if (status)
  268. break;
  269. dfprintk(DIRCACHE, "NFS: found cookie %Lu at index %Ld\n",
  270. (unsigned long long)entry->cookie, desc->current_index);
  271. if (desc->file->f_pos == desc->current_index) {
  272. *desc->dir_cookie = entry->cookie;
  273. break;
  274. }
  275. desc->current_index++;
  276. if (loop_count++ > 200) {
  277. loop_count = 0;
  278. schedule();
  279. }
  280. }
  281. return status;
  282. }
  283. /*
  284. * Find the given page, and call find_dirent() or find_dirent_index in
  285. * order to try to return the next entry.
  286. */
  287. static inline
  288. int find_dirent_page(nfs_readdir_descriptor_t *desc)
  289. {
  290. struct inode *inode = desc->file->f_path.dentry->d_inode;
  291. struct page *page;
  292. int status;
  293. dfprintk(DIRCACHE, "NFS: %s: searching page %ld for target %Lu\n",
  294. __func__, desc->page_index,
  295. (long long) *desc->dir_cookie);
  296. /* If we find the page in the page_cache, we cannot be sure
  297. * how fresh the data is, so we will ignore readdir_plus attributes.
  298. */
  299. desc->timestamp_valid = 0;
  300. page = read_cache_page(inode->i_mapping, desc->page_index,
  301. (filler_t *)nfs_readdir_filler, desc);
  302. if (IS_ERR(page)) {
  303. status = PTR_ERR(page);
  304. goto out;
  305. }
  306. /* NOTE: Someone else may have changed the READDIRPLUS flag */
  307. desc->page = page;
  308. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  309. if (*desc->dir_cookie != 0)
  310. status = find_dirent(desc);
  311. else
  312. status = find_dirent_index(desc);
  313. if (status < 0)
  314. dir_page_release(desc);
  315. out:
  316. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, status);
  317. return status;
  318. }
  319. /*
  320. * Recurse through the page cache pages, and return a
  321. * filled nfs_entry structure of the next directory entry if possible.
  322. *
  323. * The target for the search is '*desc->dir_cookie' if non-0,
  324. * 'desc->file->f_pos' otherwise
  325. */
  326. static inline
  327. int readdir_search_pagecache(nfs_readdir_descriptor_t *desc)
  328. {
  329. int loop_count = 0;
  330. int res;
  331. /* Always search-by-index from the beginning of the cache */
  332. if (*desc->dir_cookie == 0) {
  333. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for offset %Ld\n",
  334. (long long)desc->file->f_pos);
  335. desc->page_index = 0;
  336. desc->entry->cookie = desc->entry->prev_cookie = 0;
  337. desc->entry->eof = 0;
  338. desc->current_index = 0;
  339. } else
  340. dfprintk(DIRCACHE, "NFS: readdir_search_pagecache() searching for cookie %Lu\n",
  341. (unsigned long long)*desc->dir_cookie);
  342. for (;;) {
  343. res = find_dirent_page(desc);
  344. if (res != -EAGAIN)
  345. break;
  346. /* Align to beginning of next page */
  347. desc->page_index ++;
  348. if (loop_count++ > 200) {
  349. loop_count = 0;
  350. schedule();
  351. }
  352. }
  353. dfprintk(DIRCACHE, "NFS: %s: returns %d\n", __func__, res);
  354. return res;
  355. }
  356. static inline unsigned int dt_type(struct inode *inode)
  357. {
  358. return (inode->i_mode >> 12) & 15;
  359. }
  360. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc);
  361. /*
  362. * Once we've found the start of the dirent within a page: fill 'er up...
  363. */
  364. static
  365. int nfs_do_filldir(nfs_readdir_descriptor_t *desc, void *dirent,
  366. filldir_t filldir)
  367. {
  368. struct file *file = desc->file;
  369. struct nfs_entry *entry = desc->entry;
  370. struct dentry *dentry = NULL;
  371. u64 fileid;
  372. int loop_count = 0,
  373. res;
  374. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling starting @ cookie %Lu\n",
  375. (unsigned long long)entry->cookie);
  376. for(;;) {
  377. unsigned d_type = DT_UNKNOWN;
  378. /* Note: entry->prev_cookie contains the cookie for
  379. * retrieving the current dirent on the server */
  380. fileid = entry->ino;
  381. /* Get a dentry if we have one */
  382. if (dentry != NULL)
  383. dput(dentry);
  384. dentry = nfs_readdir_lookup(desc);
  385. /* Use readdirplus info */
  386. if (dentry != NULL && dentry->d_inode != NULL) {
  387. d_type = dt_type(dentry->d_inode);
  388. fileid = NFS_FILEID(dentry->d_inode);
  389. }
  390. res = filldir(dirent, entry->name, entry->len,
  391. file->f_pos, nfs_compat_user_ino64(fileid),
  392. d_type);
  393. if (res < 0)
  394. break;
  395. file->f_pos++;
  396. *desc->dir_cookie = entry->cookie;
  397. if (dir_decode(desc) != 0) {
  398. desc->page_index ++;
  399. break;
  400. }
  401. if (loop_count++ > 200) {
  402. loop_count = 0;
  403. schedule();
  404. }
  405. }
  406. dir_page_release(desc);
  407. if (dentry != NULL)
  408. dput(dentry);
  409. dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %Lu; returning = %d\n",
  410. (unsigned long long)*desc->dir_cookie, res);
  411. return res;
  412. }
  413. /*
  414. * If we cannot find a cookie in our cache, we suspect that this is
  415. * because it points to a deleted file, so we ask the server to return
  416. * whatever it thinks is the next entry. We then feed this to filldir.
  417. * If all goes well, we should then be able to find our way round the
  418. * cache on the next call to readdir_search_pagecache();
  419. *
  420. * NOTE: we cannot add the anonymous page to the pagecache because
  421. * the data it contains might not be page aligned. Besides,
  422. * we should already have a complete representation of the
  423. * directory in the page cache by the time we get here.
  424. */
  425. static inline
  426. int uncached_readdir(nfs_readdir_descriptor_t *desc, void *dirent,
  427. filldir_t filldir)
  428. {
  429. struct file *file = desc->file;
  430. struct inode *inode = file->f_path.dentry->d_inode;
  431. struct rpc_cred *cred = nfs_file_cred(file);
  432. struct page *page = NULL;
  433. int status;
  434. unsigned long timestamp, gencount;
  435. dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n",
  436. (unsigned long long)*desc->dir_cookie);
  437. page = alloc_page(GFP_HIGHUSER);
  438. if (!page) {
  439. status = -ENOMEM;
  440. goto out;
  441. }
  442. timestamp = jiffies;
  443. gencount = nfs_inc_attr_generation_counter();
  444. status = NFS_PROTO(inode)->readdir(file->f_path.dentry, cred,
  445. *desc->dir_cookie, page,
  446. NFS_SERVER(inode)->dtsize,
  447. desc->plus);
  448. desc->page = page;
  449. desc->ptr = kmap(page); /* matching kunmap in nfs_do_filldir */
  450. if (status >= 0) {
  451. desc->timestamp = timestamp;
  452. desc->gencount = gencount;
  453. desc->timestamp_valid = 1;
  454. if ((status = dir_decode(desc)) == 0)
  455. desc->entry->prev_cookie = *desc->dir_cookie;
  456. } else
  457. status = -EIO;
  458. if (status < 0)
  459. goto out_release;
  460. status = nfs_do_filldir(desc, dirent, filldir);
  461. /* Reset read descriptor so it searches the page cache from
  462. * the start upon the next call to readdir_search_pagecache() */
  463. desc->page_index = 0;
  464. desc->entry->cookie = desc->entry->prev_cookie = 0;
  465. desc->entry->eof = 0;
  466. out:
  467. dfprintk(DIRCACHE, "NFS: %s: returns %d\n",
  468. __func__, status);
  469. return status;
  470. out_release:
  471. dir_page_release(desc);
  472. goto out;
  473. }
  474. /* The file offset position represents the dirent entry number. A
  475. last cookie cache takes care of the common case of reading the
  476. whole directory.
  477. */
  478. static int nfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
  479. {
  480. struct dentry *dentry = filp->f_path.dentry;
  481. struct inode *inode = dentry->d_inode;
  482. nfs_readdir_descriptor_t my_desc,
  483. *desc = &my_desc;
  484. struct nfs_entry my_entry;
  485. int res = -ENOMEM;
  486. dfprintk(FILE, "NFS: readdir(%s/%s) starting at cookie %llu\n",
  487. dentry->d_parent->d_name.name, dentry->d_name.name,
  488. (long long)filp->f_pos);
  489. nfs_inc_stats(inode, NFSIOS_VFSGETDENTS);
  490. /*
  491. * filp->f_pos points to the dirent entry number.
  492. * *desc->dir_cookie has the cookie for the next entry. We have
  493. * to either find the entry with the appropriate number or
  494. * revalidate the cookie.
  495. */
  496. memset(desc, 0, sizeof(*desc));
  497. desc->file = filp;
  498. desc->dir_cookie = &nfs_file_open_context(filp)->dir_cookie;
  499. desc->decode = NFS_PROTO(inode)->decode_dirent;
  500. desc->plus = NFS_USE_READDIRPLUS(inode);
  501. my_entry.cookie = my_entry.prev_cookie = 0;
  502. my_entry.eof = 0;
  503. my_entry.fh = nfs_alloc_fhandle();
  504. my_entry.fattr = nfs_alloc_fattr();
  505. if (my_entry.fh == NULL || my_entry.fattr == NULL)
  506. goto out_alloc_failed;
  507. desc->entry = &my_entry;
  508. nfs_block_sillyrename(dentry);
  509. res = nfs_revalidate_mapping(inode, filp->f_mapping);
  510. if (res < 0)
  511. goto out;
  512. while(!desc->entry->eof) {
  513. res = readdir_search_pagecache(desc);
  514. if (res == -EBADCOOKIE) {
  515. /* This means either end of directory */
  516. if (*desc->dir_cookie && desc->entry->cookie != *desc->dir_cookie) {
  517. /* Or that the server has 'lost' a cookie */
  518. res = uncached_readdir(desc, dirent, filldir);
  519. if (res >= 0)
  520. continue;
  521. }
  522. res = 0;
  523. break;
  524. }
  525. if (res == -ETOOSMALL && desc->plus) {
  526. clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags);
  527. nfs_zap_caches(inode);
  528. desc->plus = 0;
  529. desc->entry->eof = 0;
  530. continue;
  531. }
  532. if (res < 0)
  533. break;
  534. res = nfs_do_filldir(desc, dirent, filldir);
  535. if (res < 0) {
  536. res = 0;
  537. break;
  538. }
  539. }
  540. out:
  541. nfs_unblock_sillyrename(dentry);
  542. if (res > 0)
  543. res = 0;
  544. out_alloc_failed:
  545. nfs_free_fattr(my_entry.fattr);
  546. nfs_free_fhandle(my_entry.fh);
  547. dfprintk(FILE, "NFS: readdir(%s/%s) returns %d\n",
  548. dentry->d_parent->d_name.name, dentry->d_name.name,
  549. res);
  550. return res;
  551. }
  552. static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int origin)
  553. {
  554. struct dentry *dentry = filp->f_path.dentry;
  555. struct inode *inode = dentry->d_inode;
  556. dfprintk(FILE, "NFS: llseek dir(%s/%s, %lld, %d)\n",
  557. dentry->d_parent->d_name.name,
  558. dentry->d_name.name,
  559. offset, origin);
  560. mutex_lock(&inode->i_mutex);
  561. switch (origin) {
  562. case 1:
  563. offset += filp->f_pos;
  564. case 0:
  565. if (offset >= 0)
  566. break;
  567. default:
  568. offset = -EINVAL;
  569. goto out;
  570. }
  571. if (offset != filp->f_pos) {
  572. filp->f_pos = offset;
  573. nfs_file_open_context(filp)->dir_cookie = 0;
  574. }
  575. out:
  576. mutex_unlock(&inode->i_mutex);
  577. return offset;
  578. }
  579. /*
  580. * All directory operations under NFS are synchronous, so fsync()
  581. * is a dummy operation.
  582. */
  583. static int nfs_fsync_dir(struct file *filp, int datasync)
  584. {
  585. struct dentry *dentry = filp->f_path.dentry;
  586. dfprintk(FILE, "NFS: fsync dir(%s/%s) datasync %d\n",
  587. dentry->d_parent->d_name.name, dentry->d_name.name,
  588. datasync);
  589. nfs_inc_stats(dentry->d_inode, NFSIOS_VFSFSYNC);
  590. return 0;
  591. }
  592. /**
  593. * nfs_force_lookup_revalidate - Mark the directory as having changed
  594. * @dir - pointer to directory inode
  595. *
  596. * This forces the revalidation code in nfs_lookup_revalidate() to do a
  597. * full lookup on all child dentries of 'dir' whenever a change occurs
  598. * on the server that might have invalidated our dcache.
  599. *
  600. * The caller should be holding dir->i_lock
  601. */
  602. void nfs_force_lookup_revalidate(struct inode *dir)
  603. {
  604. NFS_I(dir)->cache_change_attribute++;
  605. }
  606. /*
  607. * A check for whether or not the parent directory has changed.
  608. * In the case it has, we assume that the dentries are untrustworthy
  609. * and may need to be looked up again.
  610. */
  611. static int nfs_check_verifier(struct inode *dir, struct dentry *dentry)
  612. {
  613. if (IS_ROOT(dentry))
  614. return 1;
  615. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
  616. return 0;
  617. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  618. return 0;
  619. /* Revalidate nfsi->cache_change_attribute before we declare a match */
  620. if (nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0)
  621. return 0;
  622. if (!nfs_verify_change_attribute(dir, dentry->d_time))
  623. return 0;
  624. return 1;
  625. }
  626. /*
  627. * Return the intent data that applies to this particular path component
  628. *
  629. * Note that the current set of intents only apply to the very last
  630. * component of the path.
  631. * We check for this using LOOKUP_CONTINUE and LOOKUP_PARENT.
  632. */
  633. static inline unsigned int nfs_lookup_check_intent(struct nameidata *nd, unsigned int mask)
  634. {
  635. if (nd->flags & (LOOKUP_CONTINUE|LOOKUP_PARENT))
  636. return 0;
  637. return nd->flags & mask;
  638. }
  639. /*
  640. * Use intent information to check whether or not we're going to do
  641. * an O_EXCL create using this path component.
  642. */
  643. static int nfs_is_exclusive_create(struct inode *dir, struct nameidata *nd)
  644. {
  645. if (NFS_PROTO(dir)->version == 2)
  646. return 0;
  647. return nd && nfs_lookup_check_intent(nd, LOOKUP_EXCL);
  648. }
  649. /*
  650. * Inode and filehandle revalidation for lookups.
  651. *
  652. * We force revalidation in the cases where the VFS sets LOOKUP_REVAL,
  653. * or if the intent information indicates that we're about to open this
  654. * particular file and the "nocto" mount flag is not set.
  655. *
  656. */
  657. static inline
  658. int nfs_lookup_verify_inode(struct inode *inode, struct nameidata *nd)
  659. {
  660. struct nfs_server *server = NFS_SERVER(inode);
  661. if (test_bit(NFS_INO_MOUNTPOINT, &NFS_I(inode)->flags))
  662. return 0;
  663. if (nd != NULL) {
  664. /* VFS wants an on-the-wire revalidation */
  665. if (nd->flags & LOOKUP_REVAL)
  666. goto out_force;
  667. /* This is an open(2) */
  668. if (nfs_lookup_check_intent(nd, LOOKUP_OPEN) != 0 &&
  669. !(server->flags & NFS_MOUNT_NOCTO) &&
  670. (S_ISREG(inode->i_mode) ||
  671. S_ISDIR(inode->i_mode)))
  672. goto out_force;
  673. return 0;
  674. }
  675. return nfs_revalidate_inode(server, inode);
  676. out_force:
  677. return __nfs_revalidate_inode(server, inode);
  678. }
  679. /*
  680. * We judge how long we want to trust negative
  681. * dentries by looking at the parent inode mtime.
  682. *
  683. * If parent mtime has changed, we revalidate, else we wait for a
  684. * period corresponding to the parent's attribute cache timeout value.
  685. */
  686. static inline
  687. int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry,
  688. struct nameidata *nd)
  689. {
  690. /* Don't revalidate a negative dentry if we're creating a new file */
  691. if (nd != NULL && nfs_lookup_check_intent(nd, LOOKUP_CREATE) != 0)
  692. return 0;
  693. if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG)
  694. return 1;
  695. return !nfs_check_verifier(dir, dentry);
  696. }
  697. /*
  698. * This is called every time the dcache has a lookup hit,
  699. * and we should check whether we can really trust that
  700. * lookup.
  701. *
  702. * NOTE! The hit can be a negative hit too, don't assume
  703. * we have an inode!
  704. *
  705. * If the parent directory is seen to have changed, we throw out the
  706. * cached dentry and do a new lookup.
  707. */
  708. static int nfs_lookup_revalidate(struct dentry * dentry, struct nameidata *nd)
  709. {
  710. struct inode *dir;
  711. struct inode *inode;
  712. struct dentry *parent;
  713. struct nfs_fh *fhandle = NULL;
  714. struct nfs_fattr *fattr = NULL;
  715. int error;
  716. parent = dget_parent(dentry);
  717. dir = parent->d_inode;
  718. nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE);
  719. inode = dentry->d_inode;
  720. if (!inode) {
  721. if (nfs_neg_need_reval(dir, dentry, nd))
  722. goto out_bad;
  723. goto out_valid;
  724. }
  725. if (is_bad_inode(inode)) {
  726. dfprintk(LOOKUPCACHE, "%s: %s/%s has dud inode\n",
  727. __func__, dentry->d_parent->d_name.name,
  728. dentry->d_name.name);
  729. goto out_bad;
  730. }
  731. if (nfs_have_delegation(inode, FMODE_READ))
  732. goto out_set_verifier;
  733. /* Force a full look up iff the parent directory has changed */
  734. if (!nfs_is_exclusive_create(dir, nd) && nfs_check_verifier(dir, dentry)) {
  735. if (nfs_lookup_verify_inode(inode, nd))
  736. goto out_zap_parent;
  737. goto out_valid;
  738. }
  739. if (NFS_STALE(inode))
  740. goto out_bad;
  741. error = -ENOMEM;
  742. fhandle = nfs_alloc_fhandle();
  743. fattr = nfs_alloc_fattr();
  744. if (fhandle == NULL || fattr == NULL)
  745. goto out_error;
  746. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  747. if (error)
  748. goto out_bad;
  749. if (nfs_compare_fh(NFS_FH(inode), fhandle))
  750. goto out_bad;
  751. if ((error = nfs_refresh_inode(inode, fattr)) != 0)
  752. goto out_bad;
  753. nfs_free_fattr(fattr);
  754. nfs_free_fhandle(fhandle);
  755. out_set_verifier:
  756. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  757. out_valid:
  758. dput(parent);
  759. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is valid\n",
  760. __func__, dentry->d_parent->d_name.name,
  761. dentry->d_name.name);
  762. return 1;
  763. out_zap_parent:
  764. nfs_zap_caches(dir);
  765. out_bad:
  766. nfs_mark_for_revalidate(dir);
  767. if (inode && S_ISDIR(inode->i_mode)) {
  768. /* Purge readdir caches. */
  769. nfs_zap_caches(inode);
  770. /* If we have submounts, don't unhash ! */
  771. if (have_submounts(dentry))
  772. goto out_valid;
  773. if (dentry->d_flags & DCACHE_DISCONNECTED)
  774. goto out_valid;
  775. shrink_dcache_parent(dentry);
  776. }
  777. d_drop(dentry);
  778. nfs_free_fattr(fattr);
  779. nfs_free_fhandle(fhandle);
  780. dput(parent);
  781. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) is invalid\n",
  782. __func__, dentry->d_parent->d_name.name,
  783. dentry->d_name.name);
  784. return 0;
  785. out_error:
  786. nfs_free_fattr(fattr);
  787. nfs_free_fhandle(fhandle);
  788. dput(parent);
  789. dfprintk(LOOKUPCACHE, "NFS: %s(%s/%s) lookup returned error %d\n",
  790. __func__, dentry->d_parent->d_name.name,
  791. dentry->d_name.name, error);
  792. return error;
  793. }
  794. /*
  795. * This is called from dput() when d_count is going to 0.
  796. */
  797. static int nfs_dentry_delete(struct dentry *dentry)
  798. {
  799. dfprintk(VFS, "NFS: dentry_delete(%s/%s, %x)\n",
  800. dentry->d_parent->d_name.name, dentry->d_name.name,
  801. dentry->d_flags);
  802. /* Unhash any dentry with a stale inode */
  803. if (dentry->d_inode != NULL && NFS_STALE(dentry->d_inode))
  804. return 1;
  805. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  806. /* Unhash it, so that ->d_iput() would be called */
  807. return 1;
  808. }
  809. if (!(dentry->d_sb->s_flags & MS_ACTIVE)) {
  810. /* Unhash it, so that ancestors of killed async unlink
  811. * files will be cleaned up during umount */
  812. return 1;
  813. }
  814. return 0;
  815. }
  816. static void nfs_drop_nlink(struct inode *inode)
  817. {
  818. spin_lock(&inode->i_lock);
  819. if (inode->i_nlink > 0)
  820. drop_nlink(inode);
  821. spin_unlock(&inode->i_lock);
  822. }
  823. /*
  824. * Called when the dentry loses inode.
  825. * We use it to clean up silly-renamed files.
  826. */
  827. static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode)
  828. {
  829. if (S_ISDIR(inode->i_mode))
  830. /* drop any readdir cache as it could easily be old */
  831. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA;
  832. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  833. drop_nlink(inode);
  834. nfs_complete_unlink(dentry, inode);
  835. }
  836. iput(inode);
  837. }
  838. const struct dentry_operations nfs_dentry_operations = {
  839. .d_revalidate = nfs_lookup_revalidate,
  840. .d_delete = nfs_dentry_delete,
  841. .d_iput = nfs_dentry_iput,
  842. };
  843. static struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, struct nameidata *nd)
  844. {
  845. struct dentry *res;
  846. struct dentry *parent;
  847. struct inode *inode = NULL;
  848. struct nfs_fh *fhandle = NULL;
  849. struct nfs_fattr *fattr = NULL;
  850. int error;
  851. dfprintk(VFS, "NFS: lookup(%s/%s)\n",
  852. dentry->d_parent->d_name.name, dentry->d_name.name);
  853. nfs_inc_stats(dir, NFSIOS_VFSLOOKUP);
  854. res = ERR_PTR(-ENAMETOOLONG);
  855. if (dentry->d_name.len > NFS_SERVER(dir)->namelen)
  856. goto out;
  857. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  858. /*
  859. * If we're doing an exclusive create, optimize away the lookup
  860. * but don't hash the dentry.
  861. */
  862. if (nfs_is_exclusive_create(dir, nd)) {
  863. d_instantiate(dentry, NULL);
  864. res = NULL;
  865. goto out;
  866. }
  867. res = ERR_PTR(-ENOMEM);
  868. fhandle = nfs_alloc_fhandle();
  869. fattr = nfs_alloc_fattr();
  870. if (fhandle == NULL || fattr == NULL)
  871. goto out;
  872. parent = dentry->d_parent;
  873. /* Protect against concurrent sillydeletes */
  874. nfs_block_sillyrename(parent);
  875. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  876. if (error == -ENOENT)
  877. goto no_entry;
  878. if (error < 0) {
  879. res = ERR_PTR(error);
  880. goto out_unblock_sillyrename;
  881. }
  882. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  883. res = (struct dentry *)inode;
  884. if (IS_ERR(res))
  885. goto out_unblock_sillyrename;
  886. no_entry:
  887. res = d_materialise_unique(dentry, inode);
  888. if (res != NULL) {
  889. if (IS_ERR(res))
  890. goto out_unblock_sillyrename;
  891. dentry = res;
  892. }
  893. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  894. out_unblock_sillyrename:
  895. nfs_unblock_sillyrename(parent);
  896. out:
  897. nfs_free_fattr(fattr);
  898. nfs_free_fhandle(fhandle);
  899. return res;
  900. }
  901. #ifdef CONFIG_NFS_V4
  902. static int nfs_open_revalidate(struct dentry *, struct nameidata *);
  903. const struct dentry_operations nfs4_dentry_operations = {
  904. .d_revalidate = nfs_open_revalidate,
  905. .d_delete = nfs_dentry_delete,
  906. .d_iput = nfs_dentry_iput,
  907. };
  908. /*
  909. * Use intent information to determine whether we need to substitute
  910. * the NFSv4-style stateful OPEN for the LOOKUP call
  911. */
  912. static int is_atomic_open(struct nameidata *nd)
  913. {
  914. if (nd == NULL || nfs_lookup_check_intent(nd, LOOKUP_OPEN) == 0)
  915. return 0;
  916. /* NFS does not (yet) have a stateful open for directories */
  917. if (nd->flags & LOOKUP_DIRECTORY)
  918. return 0;
  919. /* Are we trying to write to a read only partition? */
  920. if (__mnt_is_readonly(nd->path.mnt) &&
  921. (nd->intent.open.flags & (O_CREAT|O_TRUNC|FMODE_WRITE)))
  922. return 0;
  923. return 1;
  924. }
  925. static struct dentry *nfs_atomic_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  926. {
  927. struct dentry *res = NULL;
  928. int error;
  929. dfprintk(VFS, "NFS: atomic_lookup(%s/%ld), %s\n",
  930. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  931. /* Check that we are indeed trying to open this file */
  932. if (!is_atomic_open(nd))
  933. goto no_open;
  934. if (dentry->d_name.len > NFS_SERVER(dir)->namelen) {
  935. res = ERR_PTR(-ENAMETOOLONG);
  936. goto out;
  937. }
  938. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  939. /* Let vfs_create() deal with O_EXCL. Instantiate, but don't hash
  940. * the dentry. */
  941. if (nd->flags & LOOKUP_EXCL) {
  942. d_instantiate(dentry, NULL);
  943. goto out;
  944. }
  945. /* Open the file on the server */
  946. res = nfs4_atomic_open(dir, dentry, nd);
  947. if (IS_ERR(res)) {
  948. error = PTR_ERR(res);
  949. switch (error) {
  950. /* Make a negative dentry */
  951. case -ENOENT:
  952. res = NULL;
  953. goto out;
  954. /* This turned out not to be a regular file */
  955. case -EISDIR:
  956. case -ENOTDIR:
  957. goto no_open;
  958. case -ELOOP:
  959. if (!(nd->intent.open.flags & O_NOFOLLOW))
  960. goto no_open;
  961. /* case -EINVAL: */
  962. default:
  963. goto out;
  964. }
  965. } else if (res != NULL)
  966. dentry = res;
  967. out:
  968. return res;
  969. no_open:
  970. return nfs_lookup(dir, dentry, nd);
  971. }
  972. static int nfs_open_revalidate(struct dentry *dentry, struct nameidata *nd)
  973. {
  974. struct dentry *parent = NULL;
  975. struct inode *inode = dentry->d_inode;
  976. struct inode *dir;
  977. int openflags, ret = 0;
  978. if (!is_atomic_open(nd) || d_mountpoint(dentry))
  979. goto no_open;
  980. parent = dget_parent(dentry);
  981. dir = parent->d_inode;
  982. /* We can't create new files in nfs_open_revalidate(), so we
  983. * optimize away revalidation of negative dentries.
  984. */
  985. if (inode == NULL) {
  986. if (!nfs_neg_need_reval(dir, dentry, nd))
  987. ret = 1;
  988. goto out;
  989. }
  990. /* NFS only supports OPEN on regular files */
  991. if (!S_ISREG(inode->i_mode))
  992. goto no_open_dput;
  993. openflags = nd->intent.open.flags;
  994. /* We cannot do exclusive creation on a positive dentry */
  995. if ((openflags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  996. goto no_open_dput;
  997. /* We can't create new files, or truncate existing ones here */
  998. openflags &= ~(O_CREAT|O_TRUNC);
  999. /*
  1000. * Note: we're not holding inode->i_mutex and so may be racing with
  1001. * operations that change the directory. We therefore save the
  1002. * change attribute *before* we do the RPC call.
  1003. */
  1004. ret = nfs4_open_revalidate(dir, dentry, openflags, nd);
  1005. out:
  1006. dput(parent);
  1007. if (!ret)
  1008. d_drop(dentry);
  1009. return ret;
  1010. no_open_dput:
  1011. dput(parent);
  1012. no_open:
  1013. return nfs_lookup_revalidate(dentry, nd);
  1014. }
  1015. #endif /* CONFIG_NFSV4 */
  1016. static struct dentry *nfs_readdir_lookup(nfs_readdir_descriptor_t *desc)
  1017. {
  1018. struct dentry *parent = desc->file->f_path.dentry;
  1019. struct inode *dir = parent->d_inode;
  1020. struct nfs_entry *entry = desc->entry;
  1021. struct dentry *dentry, *alias;
  1022. struct qstr name = {
  1023. .name = entry->name,
  1024. .len = entry->len,
  1025. };
  1026. struct inode *inode;
  1027. unsigned long verf = nfs_save_change_attribute(dir);
  1028. switch (name.len) {
  1029. case 2:
  1030. if (name.name[0] == '.' && name.name[1] == '.')
  1031. return dget_parent(parent);
  1032. break;
  1033. case 1:
  1034. if (name.name[0] == '.')
  1035. return dget(parent);
  1036. }
  1037. spin_lock(&dir->i_lock);
  1038. if (NFS_I(dir)->cache_validity & NFS_INO_INVALID_DATA) {
  1039. spin_unlock(&dir->i_lock);
  1040. return NULL;
  1041. }
  1042. spin_unlock(&dir->i_lock);
  1043. name.hash = full_name_hash(name.name, name.len);
  1044. dentry = d_lookup(parent, &name);
  1045. if (dentry != NULL) {
  1046. /* Is this a positive dentry that matches the readdir info? */
  1047. if (dentry->d_inode != NULL &&
  1048. (NFS_FILEID(dentry->d_inode) == entry->ino ||
  1049. d_mountpoint(dentry))) {
  1050. if (!desc->plus || entry->fh->size == 0)
  1051. return dentry;
  1052. if (nfs_compare_fh(NFS_FH(dentry->d_inode),
  1053. entry->fh) == 0)
  1054. goto out_renew;
  1055. }
  1056. /* No, so d_drop to allow one to be created */
  1057. d_drop(dentry);
  1058. dput(dentry);
  1059. }
  1060. if (!desc->plus || !(entry->fattr->valid & NFS_ATTR_FATTR))
  1061. return NULL;
  1062. if (name.len > NFS_SERVER(dir)->namelen)
  1063. return NULL;
  1064. /* Note: caller is already holding the dir->i_mutex! */
  1065. dentry = d_alloc(parent, &name);
  1066. if (dentry == NULL)
  1067. return NULL;
  1068. dentry->d_op = NFS_PROTO(dir)->dentry_ops;
  1069. inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr);
  1070. if (IS_ERR(inode)) {
  1071. dput(dentry);
  1072. return NULL;
  1073. }
  1074. alias = d_materialise_unique(dentry, inode);
  1075. if (alias != NULL) {
  1076. dput(dentry);
  1077. if (IS_ERR(alias))
  1078. return NULL;
  1079. dentry = alias;
  1080. }
  1081. out_renew:
  1082. nfs_set_verifier(dentry, verf);
  1083. return dentry;
  1084. }
  1085. /*
  1086. * Code common to create, mkdir, and mknod.
  1087. */
  1088. int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle,
  1089. struct nfs_fattr *fattr)
  1090. {
  1091. struct dentry *parent = dget_parent(dentry);
  1092. struct inode *dir = parent->d_inode;
  1093. struct inode *inode;
  1094. int error = -EACCES;
  1095. d_drop(dentry);
  1096. /* We may have been initialized further down */
  1097. if (dentry->d_inode)
  1098. goto out;
  1099. if (fhandle->size == 0) {
  1100. error = NFS_PROTO(dir)->lookup(dir, &dentry->d_name, fhandle, fattr);
  1101. if (error)
  1102. goto out_error;
  1103. }
  1104. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1105. if (!(fattr->valid & NFS_ATTR_FATTR)) {
  1106. struct nfs_server *server = NFS_SB(dentry->d_sb);
  1107. error = server->nfs_client->rpc_ops->getattr(server, fhandle, fattr);
  1108. if (error < 0)
  1109. goto out_error;
  1110. }
  1111. inode = nfs_fhget(dentry->d_sb, fhandle, fattr);
  1112. error = PTR_ERR(inode);
  1113. if (IS_ERR(inode))
  1114. goto out_error;
  1115. d_add(dentry, inode);
  1116. out:
  1117. dput(parent);
  1118. return 0;
  1119. out_error:
  1120. nfs_mark_for_revalidate(dir);
  1121. dput(parent);
  1122. return error;
  1123. }
  1124. /*
  1125. * Following a failed create operation, we drop the dentry rather
  1126. * than retain a negative dentry. This avoids a problem in the event
  1127. * that the operation succeeded on the server, but an error in the
  1128. * reply path made it appear to have failed.
  1129. */
  1130. static int nfs_create(struct inode *dir, struct dentry *dentry, int mode,
  1131. struct nameidata *nd)
  1132. {
  1133. struct iattr attr;
  1134. int error;
  1135. int open_flags = 0;
  1136. dfprintk(VFS, "NFS: create(%s/%ld), %s\n",
  1137. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1138. attr.ia_mode = mode;
  1139. attr.ia_valid = ATTR_MODE;
  1140. if ((nd->flags & LOOKUP_CREATE) != 0)
  1141. open_flags = nd->intent.open.flags;
  1142. error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags, nd);
  1143. if (error != 0)
  1144. goto out_err;
  1145. return 0;
  1146. out_err:
  1147. d_drop(dentry);
  1148. return error;
  1149. }
  1150. /*
  1151. * See comments for nfs_proc_create regarding failed operations.
  1152. */
  1153. static int
  1154. nfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev)
  1155. {
  1156. struct iattr attr;
  1157. int status;
  1158. dfprintk(VFS, "NFS: mknod(%s/%ld), %s\n",
  1159. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1160. if (!new_valid_dev(rdev))
  1161. return -EINVAL;
  1162. attr.ia_mode = mode;
  1163. attr.ia_valid = ATTR_MODE;
  1164. status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev);
  1165. if (status != 0)
  1166. goto out_err;
  1167. return 0;
  1168. out_err:
  1169. d_drop(dentry);
  1170. return status;
  1171. }
  1172. /*
  1173. * See comments for nfs_proc_create regarding failed operations.
  1174. */
  1175. static int nfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  1176. {
  1177. struct iattr attr;
  1178. int error;
  1179. dfprintk(VFS, "NFS: mkdir(%s/%ld), %s\n",
  1180. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1181. attr.ia_valid = ATTR_MODE;
  1182. attr.ia_mode = mode | S_IFDIR;
  1183. error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr);
  1184. if (error != 0)
  1185. goto out_err;
  1186. return 0;
  1187. out_err:
  1188. d_drop(dentry);
  1189. return error;
  1190. }
  1191. static void nfs_dentry_handle_enoent(struct dentry *dentry)
  1192. {
  1193. if (dentry->d_inode != NULL && !d_unhashed(dentry))
  1194. d_delete(dentry);
  1195. }
  1196. static int nfs_rmdir(struct inode *dir, struct dentry *dentry)
  1197. {
  1198. int error;
  1199. dfprintk(VFS, "NFS: rmdir(%s/%ld), %s\n",
  1200. dir->i_sb->s_id, dir->i_ino, dentry->d_name.name);
  1201. error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name);
  1202. /* Ensure the VFS deletes this inode */
  1203. if (error == 0 && dentry->d_inode != NULL)
  1204. clear_nlink(dentry->d_inode);
  1205. else if (error == -ENOENT)
  1206. nfs_dentry_handle_enoent(dentry);
  1207. return error;
  1208. }
  1209. static int nfs_sillyrename(struct inode *dir, struct dentry *dentry)
  1210. {
  1211. static unsigned int sillycounter;
  1212. const int fileidsize = sizeof(NFS_FILEID(dentry->d_inode))*2;
  1213. const int countersize = sizeof(sillycounter)*2;
  1214. const int slen = sizeof(".nfs")+fileidsize+countersize-1;
  1215. char silly[slen+1];
  1216. struct qstr qsilly;
  1217. struct dentry *sdentry;
  1218. int error = -EIO;
  1219. dfprintk(VFS, "NFS: silly-rename(%s/%s, ct=%d)\n",
  1220. dentry->d_parent->d_name.name, dentry->d_name.name,
  1221. atomic_read(&dentry->d_count));
  1222. nfs_inc_stats(dir, NFSIOS_SILLYRENAME);
  1223. /*
  1224. * We don't allow a dentry to be silly-renamed twice.
  1225. */
  1226. error = -EBUSY;
  1227. if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
  1228. goto out;
  1229. sprintf(silly, ".nfs%*.*Lx",
  1230. fileidsize, fileidsize,
  1231. (unsigned long long)NFS_FILEID(dentry->d_inode));
  1232. /* Return delegation in anticipation of the rename */
  1233. nfs_inode_return_delegation(dentry->d_inode);
  1234. sdentry = NULL;
  1235. do {
  1236. char *suffix = silly + slen - countersize;
  1237. dput(sdentry);
  1238. sillycounter++;
  1239. sprintf(suffix, "%*.*x", countersize, countersize, sillycounter);
  1240. dfprintk(VFS, "NFS: trying to rename %s to %s\n",
  1241. dentry->d_name.name, silly);
  1242. sdentry = lookup_one_len(silly, dentry->d_parent, slen);
  1243. /*
  1244. * N.B. Better to return EBUSY here ... it could be
  1245. * dangerous to delete the file while it's in use.
  1246. */
  1247. if (IS_ERR(sdentry))
  1248. goto out;
  1249. } while(sdentry->d_inode != NULL); /* need negative lookup */
  1250. qsilly.name = silly;
  1251. qsilly.len = strlen(silly);
  1252. if (dentry->d_inode) {
  1253. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1254. dir, &qsilly);
  1255. nfs_mark_for_revalidate(dentry->d_inode);
  1256. } else
  1257. error = NFS_PROTO(dir)->rename(dir, &dentry->d_name,
  1258. dir, &qsilly);
  1259. if (!error) {
  1260. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1261. d_move(dentry, sdentry);
  1262. error = nfs_async_unlink(dir, dentry);
  1263. /* If we return 0 we don't unlink */
  1264. }
  1265. dput(sdentry);
  1266. out:
  1267. return error;
  1268. }
  1269. /*
  1270. * Remove a file after making sure there are no pending writes,
  1271. * and after checking that the file has only one user.
  1272. *
  1273. * We invalidate the attribute cache and free the inode prior to the operation
  1274. * to avoid possible races if the server reuses the inode.
  1275. */
  1276. static int nfs_safe_remove(struct dentry *dentry)
  1277. {
  1278. struct inode *dir = dentry->d_parent->d_inode;
  1279. struct inode *inode = dentry->d_inode;
  1280. int error = -EBUSY;
  1281. dfprintk(VFS, "NFS: safe_remove(%s/%s)\n",
  1282. dentry->d_parent->d_name.name, dentry->d_name.name);
  1283. /* If the dentry was sillyrenamed, we simply call d_delete() */
  1284. if (dentry->d_flags & DCACHE_NFSFS_RENAMED) {
  1285. error = 0;
  1286. goto out;
  1287. }
  1288. if (inode != NULL) {
  1289. nfs_inode_return_delegation(inode);
  1290. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1291. /* The VFS may want to delete this inode */
  1292. if (error == 0)
  1293. nfs_drop_nlink(inode);
  1294. nfs_mark_for_revalidate(inode);
  1295. } else
  1296. error = NFS_PROTO(dir)->remove(dir, &dentry->d_name);
  1297. if (error == -ENOENT)
  1298. nfs_dentry_handle_enoent(dentry);
  1299. out:
  1300. return error;
  1301. }
  1302. /* We do silly rename. In case sillyrename() returns -EBUSY, the inode
  1303. * belongs to an active ".nfs..." file and we return -EBUSY.
  1304. *
  1305. * If sillyrename() returns 0, we do nothing, otherwise we unlink.
  1306. */
  1307. static int nfs_unlink(struct inode *dir, struct dentry *dentry)
  1308. {
  1309. int error;
  1310. int need_rehash = 0;
  1311. dfprintk(VFS, "NFS: unlink(%s/%ld, %s)\n", dir->i_sb->s_id,
  1312. dir->i_ino, dentry->d_name.name);
  1313. spin_lock(&dcache_lock);
  1314. spin_lock(&dentry->d_lock);
  1315. if (atomic_read(&dentry->d_count) > 1) {
  1316. spin_unlock(&dentry->d_lock);
  1317. spin_unlock(&dcache_lock);
  1318. /* Start asynchronous writeout of the inode */
  1319. write_inode_now(dentry->d_inode, 0);
  1320. error = nfs_sillyrename(dir, dentry);
  1321. return error;
  1322. }
  1323. if (!d_unhashed(dentry)) {
  1324. __d_drop(dentry);
  1325. need_rehash = 1;
  1326. }
  1327. spin_unlock(&dentry->d_lock);
  1328. spin_unlock(&dcache_lock);
  1329. error = nfs_safe_remove(dentry);
  1330. if (!error || error == -ENOENT) {
  1331. nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
  1332. } else if (need_rehash)
  1333. d_rehash(dentry);
  1334. return error;
  1335. }
  1336. /*
  1337. * To create a symbolic link, most file systems instantiate a new inode,
  1338. * add a page to it containing the path, then write it out to the disk
  1339. * using prepare_write/commit_write.
  1340. *
  1341. * Unfortunately the NFS client can't create the in-core inode first
  1342. * because it needs a file handle to create an in-core inode (see
  1343. * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the
  1344. * symlink request has completed on the server.
  1345. *
  1346. * So instead we allocate a raw page, copy the symname into it, then do
  1347. * the SYMLINK request with the page as the buffer. If it succeeds, we
  1348. * now have a new file handle and can instantiate an in-core NFS inode
  1349. * and move the raw page into its mapping.
  1350. */
  1351. static int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  1352. {
  1353. struct pagevec lru_pvec;
  1354. struct page *page;
  1355. char *kaddr;
  1356. struct iattr attr;
  1357. unsigned int pathlen = strlen(symname);
  1358. int error;
  1359. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s)\n", dir->i_sb->s_id,
  1360. dir->i_ino, dentry->d_name.name, symname);
  1361. if (pathlen > PAGE_SIZE)
  1362. return -ENAMETOOLONG;
  1363. attr.ia_mode = S_IFLNK | S_IRWXUGO;
  1364. attr.ia_valid = ATTR_MODE;
  1365. page = alloc_page(GFP_HIGHUSER);
  1366. if (!page)
  1367. return -ENOMEM;
  1368. kaddr = kmap_atomic(page, KM_USER0);
  1369. memcpy(kaddr, symname, pathlen);
  1370. if (pathlen < PAGE_SIZE)
  1371. memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen);
  1372. kunmap_atomic(kaddr, KM_USER0);
  1373. error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr);
  1374. if (error != 0) {
  1375. dfprintk(VFS, "NFS: symlink(%s/%ld, %s, %s) error %d\n",
  1376. dir->i_sb->s_id, dir->i_ino,
  1377. dentry->d_name.name, symname, error);
  1378. d_drop(dentry);
  1379. __free_page(page);
  1380. return error;
  1381. }
  1382. /*
  1383. * No big deal if we can't add this page to the page cache here.
  1384. * READLINK will get the missing page from the server if needed.
  1385. */
  1386. pagevec_init(&lru_pvec, 0);
  1387. if (!add_to_page_cache(page, dentry->d_inode->i_mapping, 0,
  1388. GFP_KERNEL)) {
  1389. pagevec_add(&lru_pvec, page);
  1390. pagevec_lru_add_file(&lru_pvec);
  1391. SetPageUptodate(page);
  1392. unlock_page(page);
  1393. } else
  1394. __free_page(page);
  1395. return 0;
  1396. }
  1397. static int
  1398. nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
  1399. {
  1400. struct inode *inode = old_dentry->d_inode;
  1401. int error;
  1402. dfprintk(VFS, "NFS: link(%s/%s -> %s/%s)\n",
  1403. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1404. dentry->d_parent->d_name.name, dentry->d_name.name);
  1405. nfs_inode_return_delegation(inode);
  1406. d_drop(dentry);
  1407. error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name);
  1408. if (error == 0) {
  1409. atomic_inc(&inode->i_count);
  1410. d_add(dentry, inode);
  1411. }
  1412. return error;
  1413. }
  1414. /*
  1415. * RENAME
  1416. * FIXME: Some nfsds, like the Linux user space nfsd, may generate a
  1417. * different file handle for the same inode after a rename (e.g. when
  1418. * moving to a different directory). A fail-safe method to do so would
  1419. * be to look up old_dir/old_name, create a link to new_dir/new_name and
  1420. * rename the old file using the sillyrename stuff. This way, the original
  1421. * file in old_dir will go away when the last process iput()s the inode.
  1422. *
  1423. * FIXED.
  1424. *
  1425. * It actually works quite well. One needs to have the possibility for
  1426. * at least one ".nfs..." file in each directory the file ever gets
  1427. * moved or linked to which happens automagically with the new
  1428. * implementation that only depends on the dcache stuff instead of
  1429. * using the inode layer
  1430. *
  1431. * Unfortunately, things are a little more complicated than indicated
  1432. * above. For a cross-directory move, we want to make sure we can get
  1433. * rid of the old inode after the operation. This means there must be
  1434. * no pending writes (if it's a file), and the use count must be 1.
  1435. * If these conditions are met, we can drop the dentries before doing
  1436. * the rename.
  1437. */
  1438. static int nfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  1439. struct inode *new_dir, struct dentry *new_dentry)
  1440. {
  1441. struct inode *old_inode = old_dentry->d_inode;
  1442. struct inode *new_inode = new_dentry->d_inode;
  1443. struct dentry *dentry = NULL, *rehash = NULL;
  1444. int error = -EBUSY;
  1445. dfprintk(VFS, "NFS: rename(%s/%s -> %s/%s, ct=%d)\n",
  1446. old_dentry->d_parent->d_name.name, old_dentry->d_name.name,
  1447. new_dentry->d_parent->d_name.name, new_dentry->d_name.name,
  1448. atomic_read(&new_dentry->d_count));
  1449. /*
  1450. * For non-directories, check whether the target is busy and if so,
  1451. * make a copy of the dentry and then do a silly-rename. If the
  1452. * silly-rename succeeds, the copied dentry is hashed and becomes
  1453. * the new target.
  1454. */
  1455. if (new_inode && !S_ISDIR(new_inode->i_mode)) {
  1456. /*
  1457. * To prevent any new references to the target during the
  1458. * rename, we unhash the dentry in advance.
  1459. */
  1460. if (!d_unhashed(new_dentry)) {
  1461. d_drop(new_dentry);
  1462. rehash = new_dentry;
  1463. }
  1464. if (atomic_read(&new_dentry->d_count) > 2) {
  1465. int err;
  1466. /* copy the target dentry's name */
  1467. dentry = d_alloc(new_dentry->d_parent,
  1468. &new_dentry->d_name);
  1469. if (!dentry)
  1470. goto out;
  1471. /* silly-rename the existing target ... */
  1472. err = nfs_sillyrename(new_dir, new_dentry);
  1473. if (err)
  1474. goto out;
  1475. new_dentry = dentry;
  1476. rehash = NULL;
  1477. new_inode = NULL;
  1478. }
  1479. }
  1480. /*
  1481. * ... prune child dentries and writebacks if needed.
  1482. */
  1483. if (atomic_read(&old_dentry->d_count) > 1) {
  1484. if (S_ISREG(old_inode->i_mode))
  1485. nfs_wb_all(old_inode);
  1486. shrink_dcache_parent(old_dentry);
  1487. }
  1488. nfs_inode_return_delegation(old_inode);
  1489. if (new_inode != NULL)
  1490. nfs_inode_return_delegation(new_inode);
  1491. error = NFS_PROTO(old_dir)->rename(old_dir, &old_dentry->d_name,
  1492. new_dir, &new_dentry->d_name);
  1493. nfs_mark_for_revalidate(old_inode);
  1494. out:
  1495. if (rehash)
  1496. d_rehash(rehash);
  1497. if (!error) {
  1498. if (new_inode != NULL)
  1499. nfs_drop_nlink(new_inode);
  1500. d_move(old_dentry, new_dentry);
  1501. nfs_set_verifier(new_dentry,
  1502. nfs_save_change_attribute(new_dir));
  1503. } else if (error == -ENOENT)
  1504. nfs_dentry_handle_enoent(old_dentry);
  1505. /* new dentry created? */
  1506. if (dentry)
  1507. dput(dentry);
  1508. return error;
  1509. }
  1510. static DEFINE_SPINLOCK(nfs_access_lru_lock);
  1511. static LIST_HEAD(nfs_access_lru_list);
  1512. static atomic_long_t nfs_access_nr_entries;
  1513. static void nfs_access_free_entry(struct nfs_access_entry *entry)
  1514. {
  1515. put_rpccred(entry->cred);
  1516. kfree(entry);
  1517. smp_mb__before_atomic_dec();
  1518. atomic_long_dec(&nfs_access_nr_entries);
  1519. smp_mb__after_atomic_dec();
  1520. }
  1521. static void nfs_access_free_list(struct list_head *head)
  1522. {
  1523. struct nfs_access_entry *cache;
  1524. while (!list_empty(head)) {
  1525. cache = list_entry(head->next, struct nfs_access_entry, lru);
  1526. list_del(&cache->lru);
  1527. nfs_access_free_entry(cache);
  1528. }
  1529. }
  1530. int nfs_access_cache_shrinker(struct shrinker *shrink, int nr_to_scan, gfp_t gfp_mask)
  1531. {
  1532. LIST_HEAD(head);
  1533. struct nfs_inode *nfsi;
  1534. struct nfs_access_entry *cache;
  1535. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  1536. return (nr_to_scan == 0) ? 0 : -1;
  1537. spin_lock(&nfs_access_lru_lock);
  1538. list_for_each_entry(nfsi, &nfs_access_lru_list, access_cache_inode_lru) {
  1539. struct inode *inode;
  1540. if (nr_to_scan-- == 0)
  1541. break;
  1542. inode = &nfsi->vfs_inode;
  1543. spin_lock(&inode->i_lock);
  1544. if (list_empty(&nfsi->access_cache_entry_lru))
  1545. goto remove_lru_entry;
  1546. cache = list_entry(nfsi->access_cache_entry_lru.next,
  1547. struct nfs_access_entry, lru);
  1548. list_move(&cache->lru, &head);
  1549. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1550. if (!list_empty(&nfsi->access_cache_entry_lru))
  1551. list_move_tail(&nfsi->access_cache_inode_lru,
  1552. &nfs_access_lru_list);
  1553. else {
  1554. remove_lru_entry:
  1555. list_del_init(&nfsi->access_cache_inode_lru);
  1556. smp_mb__before_clear_bit();
  1557. clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags);
  1558. smp_mb__after_clear_bit();
  1559. }
  1560. spin_unlock(&inode->i_lock);
  1561. }
  1562. spin_unlock(&nfs_access_lru_lock);
  1563. nfs_access_free_list(&head);
  1564. return (atomic_long_read(&nfs_access_nr_entries) / 100) * sysctl_vfs_cache_pressure;
  1565. }
  1566. static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head)
  1567. {
  1568. struct rb_root *root_node = &nfsi->access_cache;
  1569. struct rb_node *n;
  1570. struct nfs_access_entry *entry;
  1571. /* Unhook entries from the cache */
  1572. while ((n = rb_first(root_node)) != NULL) {
  1573. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1574. rb_erase(n, root_node);
  1575. list_move(&entry->lru, head);
  1576. }
  1577. nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS;
  1578. }
  1579. void nfs_access_zap_cache(struct inode *inode)
  1580. {
  1581. LIST_HEAD(head);
  1582. if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0)
  1583. return;
  1584. /* Remove from global LRU init */
  1585. spin_lock(&nfs_access_lru_lock);
  1586. if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1587. list_del_init(&NFS_I(inode)->access_cache_inode_lru);
  1588. spin_lock(&inode->i_lock);
  1589. __nfs_access_zap_cache(NFS_I(inode), &head);
  1590. spin_unlock(&inode->i_lock);
  1591. spin_unlock(&nfs_access_lru_lock);
  1592. nfs_access_free_list(&head);
  1593. }
  1594. static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, struct rpc_cred *cred)
  1595. {
  1596. struct rb_node *n = NFS_I(inode)->access_cache.rb_node;
  1597. struct nfs_access_entry *entry;
  1598. while (n != NULL) {
  1599. entry = rb_entry(n, struct nfs_access_entry, rb_node);
  1600. if (cred < entry->cred)
  1601. n = n->rb_left;
  1602. else if (cred > entry->cred)
  1603. n = n->rb_right;
  1604. else
  1605. return entry;
  1606. }
  1607. return NULL;
  1608. }
  1609. static int nfs_access_get_cached(struct inode *inode, struct rpc_cred *cred, struct nfs_access_entry *res)
  1610. {
  1611. struct nfs_inode *nfsi = NFS_I(inode);
  1612. struct nfs_access_entry *cache;
  1613. int err = -ENOENT;
  1614. spin_lock(&inode->i_lock);
  1615. if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS)
  1616. goto out_zap;
  1617. cache = nfs_access_search_rbtree(inode, cred);
  1618. if (cache == NULL)
  1619. goto out;
  1620. if (!nfs_have_delegated_attributes(inode) &&
  1621. !time_in_range_open(jiffies, cache->jiffies, cache->jiffies + nfsi->attrtimeo))
  1622. goto out_stale;
  1623. res->jiffies = cache->jiffies;
  1624. res->cred = cache->cred;
  1625. res->mask = cache->mask;
  1626. list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru);
  1627. err = 0;
  1628. out:
  1629. spin_unlock(&inode->i_lock);
  1630. return err;
  1631. out_stale:
  1632. rb_erase(&cache->rb_node, &nfsi->access_cache);
  1633. list_del(&cache->lru);
  1634. spin_unlock(&inode->i_lock);
  1635. nfs_access_free_entry(cache);
  1636. return -ENOENT;
  1637. out_zap:
  1638. spin_unlock(&inode->i_lock);
  1639. nfs_access_zap_cache(inode);
  1640. return -ENOENT;
  1641. }
  1642. static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set)
  1643. {
  1644. struct nfs_inode *nfsi = NFS_I(inode);
  1645. struct rb_root *root_node = &nfsi->access_cache;
  1646. struct rb_node **p = &root_node->rb_node;
  1647. struct rb_node *parent = NULL;
  1648. struct nfs_access_entry *entry;
  1649. spin_lock(&inode->i_lock);
  1650. while (*p != NULL) {
  1651. parent = *p;
  1652. entry = rb_entry(parent, struct nfs_access_entry, rb_node);
  1653. if (set->cred < entry->cred)
  1654. p = &parent->rb_left;
  1655. else if (set->cred > entry->cred)
  1656. p = &parent->rb_right;
  1657. else
  1658. goto found;
  1659. }
  1660. rb_link_node(&set->rb_node, parent, p);
  1661. rb_insert_color(&set->rb_node, root_node);
  1662. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1663. spin_unlock(&inode->i_lock);
  1664. return;
  1665. found:
  1666. rb_replace_node(parent, &set->rb_node, root_node);
  1667. list_add_tail(&set->lru, &nfsi->access_cache_entry_lru);
  1668. list_del(&entry->lru);
  1669. spin_unlock(&inode->i_lock);
  1670. nfs_access_free_entry(entry);
  1671. }
  1672. static void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set)
  1673. {
  1674. struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL);
  1675. if (cache == NULL)
  1676. return;
  1677. RB_CLEAR_NODE(&cache->rb_node);
  1678. cache->jiffies = set->jiffies;
  1679. cache->cred = get_rpccred(set->cred);
  1680. cache->mask = set->mask;
  1681. nfs_access_add_rbtree(inode, cache);
  1682. /* Update accounting */
  1683. smp_mb__before_atomic_inc();
  1684. atomic_long_inc(&nfs_access_nr_entries);
  1685. smp_mb__after_atomic_inc();
  1686. /* Add inode to global LRU list */
  1687. if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) {
  1688. spin_lock(&nfs_access_lru_lock);
  1689. if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags))
  1690. list_add_tail(&NFS_I(inode)->access_cache_inode_lru,
  1691. &nfs_access_lru_list);
  1692. spin_unlock(&nfs_access_lru_lock);
  1693. }
  1694. }
  1695. static int nfs_do_access(struct inode *inode, struct rpc_cred *cred, int mask)
  1696. {
  1697. struct nfs_access_entry cache;
  1698. int status;
  1699. status = nfs_access_get_cached(inode, cred, &cache);
  1700. if (status == 0)
  1701. goto out;
  1702. /* Be clever: ask server to check for all possible rights */
  1703. cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ;
  1704. cache.cred = cred;
  1705. cache.jiffies = jiffies;
  1706. status = NFS_PROTO(inode)->access(inode, &cache);
  1707. if (status != 0) {
  1708. if (status == -ESTALE) {
  1709. nfs_zap_caches(inode);
  1710. if (!S_ISDIR(inode->i_mode))
  1711. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  1712. }
  1713. return status;
  1714. }
  1715. nfs_access_add_cache(inode, &cache);
  1716. out:
  1717. if ((mask & ~cache.mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1718. return 0;
  1719. return -EACCES;
  1720. }
  1721. static int nfs_open_permission_mask(int openflags)
  1722. {
  1723. int mask = 0;
  1724. if (openflags & FMODE_READ)
  1725. mask |= MAY_READ;
  1726. if (openflags & FMODE_WRITE)
  1727. mask |= MAY_WRITE;
  1728. if (openflags & FMODE_EXEC)
  1729. mask |= MAY_EXEC;
  1730. return mask;
  1731. }
  1732. int nfs_may_open(struct inode *inode, struct rpc_cred *cred, int openflags)
  1733. {
  1734. return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags));
  1735. }
  1736. int nfs_permission(struct inode *inode, int mask)
  1737. {
  1738. struct rpc_cred *cred;
  1739. int res = 0;
  1740. nfs_inc_stats(inode, NFSIOS_VFSACCESS);
  1741. if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
  1742. goto out;
  1743. /* Is this sys_access() ? */
  1744. if (mask & (MAY_ACCESS | MAY_CHDIR))
  1745. goto force_lookup;
  1746. switch (inode->i_mode & S_IFMT) {
  1747. case S_IFLNK:
  1748. goto out;
  1749. case S_IFREG:
  1750. /* NFSv4 has atomic_open... */
  1751. if (nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)
  1752. && (mask & MAY_OPEN)
  1753. && !(mask & MAY_EXEC))
  1754. goto out;
  1755. break;
  1756. case S_IFDIR:
  1757. /*
  1758. * Optimize away all write operations, since the server
  1759. * will check permissions when we perform the op.
  1760. */
  1761. if ((mask & MAY_WRITE) && !(mask & MAY_READ))
  1762. goto out;
  1763. }
  1764. force_lookup:
  1765. if (!NFS_PROTO(inode)->access)
  1766. goto out_notsup;
  1767. cred = rpc_lookup_cred();
  1768. if (!IS_ERR(cred)) {
  1769. res = nfs_do_access(inode, cred, mask);
  1770. put_rpccred(cred);
  1771. } else
  1772. res = PTR_ERR(cred);
  1773. out:
  1774. if (!res && (mask & MAY_EXEC) && !execute_ok(inode))
  1775. res = -EACCES;
  1776. dfprintk(VFS, "NFS: permission(%s/%ld), mask=0x%x, res=%d\n",
  1777. inode->i_sb->s_id, inode->i_ino, mask, res);
  1778. return res;
  1779. out_notsup:
  1780. res = nfs_revalidate_inode(NFS_SERVER(inode), inode);
  1781. if (res == 0)
  1782. res = generic_permission(inode, mask, NULL);
  1783. goto out;
  1784. }
  1785. /*
  1786. * Local variables:
  1787. * version-control: t
  1788. * kept-new-versions: 5
  1789. * End:
  1790. */