expfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. #include <linux/fs.h>
  2. #include <linux/file.h>
  3. #include <linux/module.h>
  4. #include <linux/smp_lock.h>
  5. #include <linux/namei.h>
  6. struct export_operations export_op_default;
  7. #define CALL(ops,fun) ((ops->fun)?(ops->fun):export_op_default.fun)
  8. #define dprintk(fmt, args...) do{}while(0)
  9. static struct dentry *
  10. find_acceptable_alias(struct dentry *result,
  11. int (*acceptable)(void *context, struct dentry *dentry),
  12. void *context)
  13. {
  14. struct dentry *dentry, *toput = NULL;
  15. spin_lock(&dcache_lock);
  16. list_for_each_entry(dentry, &result->d_inode->i_dentry, d_alias) {
  17. dget_locked(dentry);
  18. spin_unlock(&dcache_lock);
  19. if (toput)
  20. dput(toput);
  21. if (dentry != result && acceptable(context, dentry)) {
  22. dput(result);
  23. return dentry;
  24. }
  25. spin_lock(&dcache_lock);
  26. toput = dentry;
  27. }
  28. spin_unlock(&dcache_lock);
  29. if (toput)
  30. dput(toput);
  31. return NULL;
  32. }
  33. /**
  34. * find_exported_dentry - helper routine to implement export_operations->decode_fh
  35. * @sb: The &super_block identifying the filesystem
  36. * @obj: An opaque identifier of the object to be found - passed to
  37. * get_inode
  38. * @parent: An optional opqaue identifier of the parent of the object.
  39. * @acceptable: A function used to test possible &dentries to see if they are
  40. * acceptable
  41. * @context: A parameter to @acceptable so that it knows on what basis to
  42. * judge.
  43. *
  44. * find_exported_dentry is the central helper routine to enable file systems
  45. * to provide the decode_fh() export_operation. It's main task is to take
  46. * an &inode, find or create an appropriate &dentry structure, and possibly
  47. * splice this into the dcache in the correct place.
  48. *
  49. * The decode_fh() operation provided by the filesystem should call
  50. * find_exported_dentry() with the same parameters that it received except
  51. * that instead of the file handle fragment, pointers to opaque identifiers
  52. * for the object and optionally its parent are passed. The default decode_fh
  53. * routine passes one pointer to the start of the filehandle fragment, and
  54. * one 8 bytes into the fragment. It is expected that most filesystems will
  55. * take this approach, though the offset to the parent identifier may well be
  56. * different.
  57. *
  58. * find_exported_dentry() will call get_dentry to get an dentry pointer from
  59. * the file system. If any &dentry in the d_alias list is acceptable, it will
  60. * be returned. Otherwise find_exported_dentry() will attempt to splice a new
  61. * &dentry into the dcache using get_name() and get_parent() to find the
  62. * appropriate place.
  63. */
  64. struct dentry *
  65. find_exported_dentry(struct super_block *sb, void *obj, void *parent,
  66. int (*acceptable)(void *context, struct dentry *de),
  67. void *context)
  68. {
  69. struct dentry *result = NULL;
  70. struct dentry *target_dir;
  71. int err;
  72. struct export_operations *nops = sb->s_export_op;
  73. struct dentry *alias;
  74. int noprogress;
  75. char nbuf[NAME_MAX+1];
  76. /*
  77. * Attempt to find the inode.
  78. */
  79. result = CALL(sb->s_export_op,get_dentry)(sb,obj);
  80. err = -ESTALE;
  81. if (result == NULL)
  82. goto err_out;
  83. if (IS_ERR(result)) {
  84. err = PTR_ERR(result);
  85. goto err_out;
  86. }
  87. if (S_ISDIR(result->d_inode->i_mode) &&
  88. (result->d_flags & DCACHE_DISCONNECTED)) {
  89. /* it is an unconnected directory, we must connect it */
  90. ;
  91. } else {
  92. if (acceptable(context, result))
  93. return result;
  94. if (S_ISDIR(result->d_inode->i_mode)) {
  95. /* there is no other dentry, so fail */
  96. goto err_result;
  97. }
  98. alias = find_acceptable_alias(result, acceptable, context);
  99. if (alias)
  100. return alias;
  101. }
  102. /* It's a directory, or we are required to confirm the file's
  103. * location in the tree based on the parent information
  104. */
  105. dprintk("find_exported_dentry: need to look harder for %s/%d\n",sb->s_id,*(int*)obj);
  106. if (S_ISDIR(result->d_inode->i_mode))
  107. target_dir = dget(result);
  108. else {
  109. if (parent == NULL)
  110. goto err_result;
  111. target_dir = CALL(sb->s_export_op,get_dentry)(sb,parent);
  112. if (IS_ERR(target_dir))
  113. err = PTR_ERR(target_dir);
  114. if (target_dir == NULL || IS_ERR(target_dir))
  115. goto err_result;
  116. }
  117. /*
  118. * Now we need to make sure that target_dir is properly connected.
  119. * It may already be, as the flag isn't always updated when connection
  120. * happens.
  121. * So, we walk up parent links until we find a connected directory,
  122. * or we run out of directories. Then we find the parent, find
  123. * the name of the child in that parent, and do a lookup.
  124. * This should connect the child into the parent
  125. * We then repeat.
  126. */
  127. /* it is possible that a confused file system might not let us complete
  128. * the path to the root. For example, if get_parent returns a directory
  129. * in which we cannot find a name for the child. While this implies a
  130. * very sick filesystem we don't want it to cause knfsd to spin. Hence
  131. * the noprogress counter. If we go through the loop 10 times (2 is
  132. * probably enough) without getting anywhere, we just give up
  133. */
  134. noprogress= 0;
  135. while (target_dir->d_flags & DCACHE_DISCONNECTED && noprogress++ < 10) {
  136. struct dentry *pd = target_dir;
  137. dget(pd);
  138. spin_lock(&pd->d_lock);
  139. while (!IS_ROOT(pd) &&
  140. (pd->d_parent->d_flags&DCACHE_DISCONNECTED)) {
  141. struct dentry *parent = pd->d_parent;
  142. dget(parent);
  143. spin_unlock(&pd->d_lock);
  144. dput(pd);
  145. pd = parent;
  146. spin_lock(&pd->d_lock);
  147. }
  148. spin_unlock(&pd->d_lock);
  149. if (!IS_ROOT(pd)) {
  150. /* must have found a connected parent - great */
  151. spin_lock(&pd->d_lock);
  152. pd->d_flags &= ~DCACHE_DISCONNECTED;
  153. spin_unlock(&pd->d_lock);
  154. noprogress = 0;
  155. } else if (pd == sb->s_root) {
  156. printk(KERN_ERR "export: Eeek filesystem root is not connected, impossible\n");
  157. spin_lock(&pd->d_lock);
  158. pd->d_flags &= ~DCACHE_DISCONNECTED;
  159. spin_unlock(&pd->d_lock);
  160. noprogress = 0;
  161. } else {
  162. /* we have hit the top of a disconnected path. Try
  163. * to find parent and connect
  164. * note: racing with some other process renaming a
  165. * directory isn't much of a problem here. If someone
  166. * renames the directory, it will end up properly
  167. * connected, which is what we want
  168. */
  169. struct dentry *ppd;
  170. struct dentry *npd;
  171. mutex_lock(&pd->d_inode->i_mutex);
  172. ppd = CALL(nops,get_parent)(pd);
  173. mutex_unlock(&pd->d_inode->i_mutex);
  174. if (IS_ERR(ppd)) {
  175. err = PTR_ERR(ppd);
  176. dprintk("find_exported_dentry: get_parent of %ld failed, err %d\n",
  177. pd->d_inode->i_ino, err);
  178. dput(pd);
  179. break;
  180. }
  181. dprintk("find_exported_dentry: find name of %lu in %lu\n", pd->d_inode->i_ino, ppd->d_inode->i_ino);
  182. err = CALL(nops,get_name)(ppd, nbuf, pd);
  183. if (err) {
  184. dput(ppd);
  185. dput(pd);
  186. if (err == -ENOENT)
  187. /* some race between get_parent and
  188. * get_name? just try again
  189. */
  190. continue;
  191. break;
  192. }
  193. dprintk("find_exported_dentry: found name: %s\n", nbuf);
  194. mutex_lock(&ppd->d_inode->i_mutex);
  195. npd = lookup_one_len(nbuf, ppd, strlen(nbuf));
  196. mutex_unlock(&ppd->d_inode->i_mutex);
  197. if (IS_ERR(npd)) {
  198. err = PTR_ERR(npd);
  199. dprintk("find_exported_dentry: lookup failed: %d\n", err);
  200. dput(ppd);
  201. dput(pd);
  202. break;
  203. }
  204. /* we didn't really want npd, we really wanted
  205. * a side-effect of the lookup.
  206. * hopefully, npd == pd, though it isn't really
  207. * a problem if it isn't
  208. */
  209. if (npd == pd)
  210. noprogress = 0;
  211. else
  212. printk("find_exported_dentry: npd != pd\n");
  213. dput(npd);
  214. dput(ppd);
  215. if (IS_ROOT(pd)) {
  216. /* something went wrong, we have to give up */
  217. dput(pd);
  218. break;
  219. }
  220. }
  221. dput(pd);
  222. }
  223. if (target_dir->d_flags & DCACHE_DISCONNECTED) {
  224. /* something went wrong - oh-well */
  225. if (!err)
  226. err = -ESTALE;
  227. goto err_target;
  228. }
  229. /* if we weren't after a directory, have one more step to go */
  230. if (result != target_dir) {
  231. struct dentry *nresult;
  232. err = CALL(nops,get_name)(target_dir, nbuf, result);
  233. if (!err) {
  234. mutex_lock(&target_dir->d_inode->i_mutex);
  235. nresult = lookup_one_len(nbuf, target_dir, strlen(nbuf));
  236. mutex_unlock(&target_dir->d_inode->i_mutex);
  237. if (!IS_ERR(nresult)) {
  238. if (nresult->d_inode) {
  239. dput(result);
  240. result = nresult;
  241. } else
  242. dput(nresult);
  243. }
  244. }
  245. }
  246. dput(target_dir);
  247. /* now result is properly connected, it is our best bet */
  248. if (acceptable(context, result))
  249. return result;
  250. alias = find_acceptable_alias(result, acceptable, context);
  251. if (alias)
  252. return alias;
  253. /* drat - I just cannot find anything acceptable */
  254. dput(result);
  255. /* It might be justifiable to return ESTALE here,
  256. * but the filehandle at-least looks reasonable good
  257. * and it just be a permission problem, so returning
  258. * -EACCESS is safer
  259. */
  260. return ERR_PTR(-EACCES);
  261. err_target:
  262. dput(target_dir);
  263. err_result:
  264. dput(result);
  265. err_out:
  266. return ERR_PTR(err);
  267. }
  268. static struct dentry *get_parent(struct dentry *child)
  269. {
  270. /* get_parent cannot be supported generically, the locking
  271. * is too icky.
  272. * instead, we just return EACCES. If server reboots or inodes
  273. * get flushed, you lose
  274. */
  275. return ERR_PTR(-EACCES);
  276. }
  277. struct getdents_callback {
  278. char *name; /* name that was found. It already points to a
  279. buffer NAME_MAX+1 is size */
  280. unsigned long ino; /* the inum we are looking for */
  281. int found; /* inode matched? */
  282. int sequence; /* sequence counter */
  283. };
  284. /*
  285. * A rather strange filldir function to capture
  286. * the name matching the specified inode number.
  287. */
  288. static int filldir_one(void * __buf, const char * name, int len,
  289. loff_t pos, ino_t ino, unsigned int d_type)
  290. {
  291. struct getdents_callback *buf = __buf;
  292. int result = 0;
  293. buf->sequence++;
  294. if (buf->ino == ino) {
  295. memcpy(buf->name, name, len);
  296. buf->name[len] = '\0';
  297. buf->found = 1;
  298. result = -1;
  299. }
  300. return result;
  301. }
  302. /**
  303. * get_name - default export_operations->get_name function
  304. * @dentry: the directory in which to find a name
  305. * @name: a pointer to a %NAME_MAX+1 char buffer to store the name
  306. * @child: the dentry for the child directory.
  307. *
  308. * calls readdir on the parent until it finds an entry with
  309. * the same inode number as the child, and returns that.
  310. */
  311. static int get_name(struct dentry *dentry, char *name,
  312. struct dentry *child)
  313. {
  314. struct inode *dir = dentry->d_inode;
  315. int error;
  316. struct file *file;
  317. struct getdents_callback buffer;
  318. error = -ENOTDIR;
  319. if (!dir || !S_ISDIR(dir->i_mode))
  320. goto out;
  321. error = -EINVAL;
  322. if (!dir->i_fop)
  323. goto out;
  324. /*
  325. * Open the directory ...
  326. */
  327. file = dentry_open(dget(dentry), NULL, O_RDONLY);
  328. error = PTR_ERR(file);
  329. if (IS_ERR(file))
  330. goto out;
  331. error = -EINVAL;
  332. if (!file->f_op->readdir)
  333. goto out_close;
  334. buffer.name = name;
  335. buffer.ino = child->d_inode->i_ino;
  336. buffer.found = 0;
  337. buffer.sequence = 0;
  338. while (1) {
  339. int old_seq = buffer.sequence;
  340. error = vfs_readdir(file, filldir_one, &buffer);
  341. if (error < 0)
  342. break;
  343. error = 0;
  344. if (buffer.found)
  345. break;
  346. error = -ENOENT;
  347. if (old_seq == buffer.sequence)
  348. break;
  349. }
  350. out_close:
  351. fput(file);
  352. out:
  353. return error;
  354. }
  355. static struct dentry *export_iget(struct super_block *sb, unsigned long ino, __u32 generation)
  356. {
  357. /* iget isn't really right if the inode is currently unallocated!!
  358. * This should really all be done inside each filesystem
  359. *
  360. * ext2fs' read_inode has been strengthed to return a bad_inode if
  361. * the inode had been deleted.
  362. *
  363. * Currently we don't know the generation for parent directory, so
  364. * a generation of 0 means "accept any"
  365. */
  366. struct inode *inode;
  367. struct dentry *result;
  368. if (ino == 0)
  369. return ERR_PTR(-ESTALE);
  370. inode = iget(sb, ino);
  371. if (inode == NULL)
  372. return ERR_PTR(-ENOMEM);
  373. if (is_bad_inode(inode)
  374. || (generation && inode->i_generation != generation)
  375. ) {
  376. /* we didn't find the right inode.. */
  377. dprintk("fh_verify: Inode %lu, Bad count: %d %d or version %u %u\n",
  378. inode->i_ino,
  379. inode->i_nlink, atomic_read(&inode->i_count),
  380. inode->i_generation,
  381. generation);
  382. iput(inode);
  383. return ERR_PTR(-ESTALE);
  384. }
  385. /* now to find a dentry.
  386. * If possible, get a well-connected one
  387. */
  388. result = d_alloc_anon(inode);
  389. if (!result) {
  390. iput(inode);
  391. return ERR_PTR(-ENOMEM);
  392. }
  393. return result;
  394. }
  395. static struct dentry *get_object(struct super_block *sb, void *vobjp)
  396. {
  397. __u32 *objp = vobjp;
  398. unsigned long ino = objp[0];
  399. __u32 generation = objp[1];
  400. return export_iget(sb, ino, generation);
  401. }
  402. /**
  403. * export_encode_fh - default export_operations->encode_fh function
  404. * @dentry: the dentry to encode
  405. * @fh: where to store the file handle fragment
  406. * @max_len: maximum length to store there
  407. * @connectable: whether to store parent information
  408. *
  409. * This default encode_fh function assumes that the 32 inode number
  410. * is suitable for locating an inode, and that the generation number
  411. * can be used to check that it is still valid. It places them in the
  412. * filehandle fragment where export_decode_fh expects to find them.
  413. */
  414. static int export_encode_fh(struct dentry *dentry, __u32 *fh, int *max_len,
  415. int connectable)
  416. {
  417. struct inode * inode = dentry->d_inode;
  418. int len = *max_len;
  419. int type = 1;
  420. if (len < 2 || (connectable && len < 4))
  421. return 255;
  422. len = 2;
  423. fh[0] = inode->i_ino;
  424. fh[1] = inode->i_generation;
  425. if (connectable && !S_ISDIR(inode->i_mode)) {
  426. struct inode *parent;
  427. spin_lock(&dentry->d_lock);
  428. parent = dentry->d_parent->d_inode;
  429. fh[2] = parent->i_ino;
  430. fh[3] = parent->i_generation;
  431. spin_unlock(&dentry->d_lock);
  432. len = 4;
  433. type = 2;
  434. }
  435. *max_len = len;
  436. return type;
  437. }
  438. /**
  439. * export_decode_fh - default export_operations->decode_fh function
  440. * @sb: The superblock
  441. * @fh: pointer to the file handle fragment
  442. * @fh_len: length of file handle fragment
  443. * @acceptable: function for testing acceptability of dentrys
  444. * @context: context for @acceptable
  445. *
  446. * This is the default decode_fh() function.
  447. * a fileid_type of 1 indicates that the filehandlefragment
  448. * just contains an object identifier understood by get_dentry.
  449. * a fileid_type of 2 says that there is also a directory
  450. * identifier 8 bytes in to the filehandlefragement.
  451. */
  452. static struct dentry *export_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
  453. int fileid_type,
  454. int (*acceptable)(void *context, struct dentry *de),
  455. void *context)
  456. {
  457. __u32 parent[2];
  458. parent[0] = parent[1] = 0;
  459. if (fh_len < 2 || fileid_type > 2)
  460. return NULL;
  461. if (fileid_type == 2) {
  462. if (fh_len > 2) parent[0] = fh[2];
  463. if (fh_len > 3) parent[1] = fh[3];
  464. }
  465. return find_exported_dentry(sb, fh, parent,
  466. acceptable, context);
  467. }
  468. struct export_operations export_op_default = {
  469. .decode_fh = export_decode_fh,
  470. .encode_fh = export_encode_fh,
  471. .get_name = get_name,
  472. .get_parent = get_parent,
  473. .get_dentry = get_object,
  474. };
  475. EXPORT_SYMBOL(export_op_default);
  476. EXPORT_SYMBOL(find_exported_dentry);
  477. MODULE_LICENSE("GPL");