dir.c 45 KB

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