dir.c 53 KB

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