dir.c 40 KB

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