super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. #include "protocol.h"
  7. #include "orangefs-kernel.h"
  8. #include "orangefs-bufmap.h"
  9. #include <linux/parser.h>
  10. /* a cache for orangefs-inode objects (i.e. orangefs inode private data) */
  11. static struct kmem_cache *orangefs_inode_cache;
  12. /* list for storing orangefs specific superblocks in use */
  13. LIST_HEAD(orangefs_superblocks);
  14. DEFINE_SPINLOCK(orangefs_superblocks_lock);
  15. enum {
  16. Opt_intr,
  17. Opt_acl,
  18. Opt_local_lock,
  19. Opt_err
  20. };
  21. static const match_table_t tokens = {
  22. { Opt_acl, "acl" },
  23. { Opt_intr, "intr" },
  24. { Opt_local_lock, "local_lock" },
  25. { Opt_err, NULL }
  26. };
  27. static int parse_mount_options(struct super_block *sb, char *options,
  28. int silent)
  29. {
  30. struct orangefs_sb_info_s *orangefs_sb = ORANGEFS_SB(sb);
  31. substring_t args[MAX_OPT_ARGS];
  32. char *p;
  33. /*
  34. * Force any potential flags that might be set from the mount
  35. * to zero, ie, initialize to unset.
  36. */
  37. sb->s_flags &= ~MS_POSIXACL;
  38. orangefs_sb->flags &= ~ORANGEFS_OPT_INTR;
  39. orangefs_sb->flags &= ~ORANGEFS_OPT_LOCAL_LOCK;
  40. while ((p = strsep(&options, ",")) != NULL) {
  41. int token;
  42. if (!*p)
  43. continue;
  44. token = match_token(p, tokens, args);
  45. switch (token) {
  46. case Opt_acl:
  47. sb->s_flags |= MS_POSIXACL;
  48. break;
  49. case Opt_intr:
  50. orangefs_sb->flags |= ORANGEFS_OPT_INTR;
  51. break;
  52. case Opt_local_lock:
  53. orangefs_sb->flags |= ORANGEFS_OPT_LOCAL_LOCK;
  54. break;
  55. default:
  56. goto fail;
  57. }
  58. }
  59. return 0;
  60. fail:
  61. if (!silent)
  62. gossip_err("Error: mount option [%s] is not supported.\n", p);
  63. return -EINVAL;
  64. }
  65. static void orangefs_inode_cache_ctor(void *req)
  66. {
  67. struct orangefs_inode_s *orangefs_inode = req;
  68. inode_init_once(&orangefs_inode->vfs_inode);
  69. init_rwsem(&orangefs_inode->xattr_sem);
  70. orangefs_inode->vfs_inode.i_version = 1;
  71. }
  72. static struct inode *orangefs_alloc_inode(struct super_block *sb)
  73. {
  74. struct orangefs_inode_s *orangefs_inode;
  75. orangefs_inode = kmem_cache_alloc(orangefs_inode_cache, GFP_KERNEL);
  76. if (orangefs_inode == NULL) {
  77. gossip_err("Failed to allocate orangefs_inode\n");
  78. return NULL;
  79. }
  80. /*
  81. * We want to clear everything except for rw_semaphore and the
  82. * vfs_inode.
  83. */
  84. memset(&orangefs_inode->refn.khandle, 0, 16);
  85. orangefs_inode->refn.fs_id = ORANGEFS_FS_ID_NULL;
  86. orangefs_inode->last_failed_block_index_read = 0;
  87. memset(orangefs_inode->link_target, 0, sizeof(orangefs_inode->link_target));
  88. orangefs_inode->pinode_flags = 0;
  89. gossip_debug(GOSSIP_SUPER_DEBUG,
  90. "orangefs_alloc_inode: allocated %p\n",
  91. &orangefs_inode->vfs_inode);
  92. return &orangefs_inode->vfs_inode;
  93. }
  94. static void orangefs_destroy_inode(struct inode *inode)
  95. {
  96. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  97. gossip_debug(GOSSIP_SUPER_DEBUG,
  98. "%s: deallocated %p destroying inode %pU\n",
  99. __func__, orangefs_inode, get_khandle_from_ino(inode));
  100. kmem_cache_free(orangefs_inode_cache, orangefs_inode);
  101. }
  102. /*
  103. * NOTE: information filled in here is typically reflected in the
  104. * output of the system command 'df'
  105. */
  106. static int orangefs_statfs(struct dentry *dentry, struct kstatfs *buf)
  107. {
  108. int ret = -ENOMEM;
  109. struct orangefs_kernel_op_s *new_op = NULL;
  110. int flags = 0;
  111. struct super_block *sb = NULL;
  112. sb = dentry->d_sb;
  113. gossip_debug(GOSSIP_SUPER_DEBUG,
  114. "orangefs_statfs: called on sb %p (fs_id is %d)\n",
  115. sb,
  116. (int)(ORANGEFS_SB(sb)->fs_id));
  117. new_op = op_alloc(ORANGEFS_VFS_OP_STATFS);
  118. if (!new_op)
  119. return ret;
  120. new_op->upcall.req.statfs.fs_id = ORANGEFS_SB(sb)->fs_id;
  121. if (ORANGEFS_SB(sb)->flags & ORANGEFS_OPT_INTR)
  122. flags = ORANGEFS_OP_INTERRUPTIBLE;
  123. ret = service_operation(new_op, "orangefs_statfs", flags);
  124. if (new_op->downcall.status < 0)
  125. goto out_op_release;
  126. gossip_debug(GOSSIP_SUPER_DEBUG,
  127. "%s: got %ld blocks available | "
  128. "%ld blocks total | %ld block size | "
  129. "%ld files total | %ld files avail\n",
  130. __func__,
  131. (long)new_op->downcall.resp.statfs.blocks_avail,
  132. (long)new_op->downcall.resp.statfs.blocks_total,
  133. (long)new_op->downcall.resp.statfs.block_size,
  134. (long)new_op->downcall.resp.statfs.files_total,
  135. (long)new_op->downcall.resp.statfs.files_avail);
  136. buf->f_type = sb->s_magic;
  137. memcpy(&buf->f_fsid, &ORANGEFS_SB(sb)->fs_id, sizeof(buf->f_fsid));
  138. buf->f_bsize = new_op->downcall.resp.statfs.block_size;
  139. buf->f_namelen = ORANGEFS_NAME_MAX;
  140. buf->f_blocks = (sector_t) new_op->downcall.resp.statfs.blocks_total;
  141. buf->f_bfree = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
  142. buf->f_bavail = (sector_t) new_op->downcall.resp.statfs.blocks_avail;
  143. buf->f_files = (sector_t) new_op->downcall.resp.statfs.files_total;
  144. buf->f_ffree = (sector_t) new_op->downcall.resp.statfs.files_avail;
  145. buf->f_frsize = sb->s_blocksize;
  146. out_op_release:
  147. op_release(new_op);
  148. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_statfs: returning %d\n", ret);
  149. return ret;
  150. }
  151. /*
  152. * Remount as initiated by VFS layer. We just need to reparse the mount
  153. * options, no need to signal pvfs2-client-core about it.
  154. */
  155. static int orangefs_remount_fs(struct super_block *sb, int *flags, char *data)
  156. {
  157. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount_fs: called\n");
  158. return parse_mount_options(sb, data, 1);
  159. }
  160. /*
  161. * Remount as initiated by pvfs2-client-core on restart. This is used to
  162. * repopulate mount information left from previous pvfs2-client-core.
  163. *
  164. * the idea here is that given a valid superblock, we're
  165. * re-initializing the user space client with the initial mount
  166. * information specified when the super block was first initialized.
  167. * this is very different than the first initialization/creation of a
  168. * superblock. we use the special service_priority_operation to make
  169. * sure that the mount gets ahead of any other pending operation that
  170. * is waiting for servicing. this means that the pvfs2-client won't
  171. * fail to start several times for all other pending operations before
  172. * the client regains all of the mount information from us.
  173. * NOTE: this function assumes that the request_mutex is already acquired!
  174. */
  175. int orangefs_remount(struct orangefs_sb_info_s *orangefs_sb)
  176. {
  177. struct orangefs_kernel_op_s *new_op;
  178. int ret = -EINVAL;
  179. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_remount: called\n");
  180. new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
  181. if (!new_op)
  182. return -ENOMEM;
  183. strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
  184. orangefs_sb->devname,
  185. ORANGEFS_MAX_SERVER_ADDR_LEN);
  186. gossip_debug(GOSSIP_SUPER_DEBUG,
  187. "Attempting ORANGEFS Remount via host %s\n",
  188. new_op->upcall.req.fs_mount.orangefs_config_server);
  189. /*
  190. * we assume that the calling function has already acquired the
  191. * request_mutex to prevent other operations from bypassing
  192. * this one
  193. */
  194. ret = service_operation(new_op, "orangefs_remount",
  195. ORANGEFS_OP_PRIORITY | ORANGEFS_OP_NO_MUTEX);
  196. gossip_debug(GOSSIP_SUPER_DEBUG,
  197. "orangefs_remount: mount got return value of %d\n",
  198. ret);
  199. if (ret == 0) {
  200. /*
  201. * store the id assigned to this sb -- it's just a
  202. * short-lived mapping that the system interface uses
  203. * to map this superblock to a particular mount entry
  204. */
  205. orangefs_sb->id = new_op->downcall.resp.fs_mount.id;
  206. orangefs_sb->mount_pending = 0;
  207. }
  208. op_release(new_op);
  209. return ret;
  210. }
  211. int fsid_key_table_initialize(void)
  212. {
  213. return 0;
  214. }
  215. void fsid_key_table_finalize(void)
  216. {
  217. }
  218. /* Called whenever the VFS dirties the inode in response to atime updates */
  219. static void orangefs_dirty_inode(struct inode *inode, int flags)
  220. {
  221. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  222. gossip_debug(GOSSIP_SUPER_DEBUG,
  223. "orangefs_dirty_inode: %pU\n",
  224. get_khandle_from_ino(inode));
  225. SetAtimeFlag(orangefs_inode);
  226. }
  227. static const struct super_operations orangefs_s_ops = {
  228. .alloc_inode = orangefs_alloc_inode,
  229. .destroy_inode = orangefs_destroy_inode,
  230. .dirty_inode = orangefs_dirty_inode,
  231. .drop_inode = generic_delete_inode,
  232. .statfs = orangefs_statfs,
  233. .remount_fs = orangefs_remount_fs,
  234. .show_options = generic_show_options,
  235. };
  236. static struct dentry *orangefs_fh_to_dentry(struct super_block *sb,
  237. struct fid *fid,
  238. int fh_len,
  239. int fh_type)
  240. {
  241. struct orangefs_object_kref refn;
  242. if (fh_len < 5 || fh_type > 2)
  243. return NULL;
  244. ORANGEFS_khandle_from(&(refn.khandle), fid->raw, 16);
  245. refn.fs_id = (u32) fid->raw[4];
  246. gossip_debug(GOSSIP_SUPER_DEBUG,
  247. "fh_to_dentry: handle %pU, fs_id %d\n",
  248. &refn.khandle,
  249. refn.fs_id);
  250. return d_obtain_alias(orangefs_iget(sb, &refn));
  251. }
  252. static int orangefs_encode_fh(struct inode *inode,
  253. __u32 *fh,
  254. int *max_len,
  255. struct inode *parent)
  256. {
  257. int len = parent ? 10 : 5;
  258. int type = 1;
  259. struct orangefs_object_kref refn;
  260. if (*max_len < len) {
  261. gossip_lerr("fh buffer is too small for encoding\n");
  262. *max_len = len;
  263. type = 255;
  264. goto out;
  265. }
  266. refn = ORANGEFS_I(inode)->refn;
  267. ORANGEFS_khandle_to(&refn.khandle, fh, 16);
  268. fh[4] = refn.fs_id;
  269. gossip_debug(GOSSIP_SUPER_DEBUG,
  270. "Encoding fh: handle %pU, fsid %u\n",
  271. &refn.khandle,
  272. refn.fs_id);
  273. if (parent) {
  274. refn = ORANGEFS_I(parent)->refn;
  275. ORANGEFS_khandle_to(&refn.khandle, (char *) fh + 20, 16);
  276. fh[9] = refn.fs_id;
  277. type = 2;
  278. gossip_debug(GOSSIP_SUPER_DEBUG,
  279. "Encoding parent: handle %pU, fsid %u\n",
  280. &refn.khandle,
  281. refn.fs_id);
  282. }
  283. *max_len = len;
  284. out:
  285. return type;
  286. }
  287. static const struct export_operations orangefs_export_ops = {
  288. .encode_fh = orangefs_encode_fh,
  289. .fh_to_dentry = orangefs_fh_to_dentry,
  290. };
  291. static int orangefs_fill_sb(struct super_block *sb,
  292. struct orangefs_fs_mount_response *fs_mount,
  293. void *data, int silent)
  294. {
  295. int ret = -EINVAL;
  296. struct inode *root = NULL;
  297. struct dentry *root_dentry = NULL;
  298. struct orangefs_object_kref root_object;
  299. /* alloc and init our private orangefs sb info */
  300. sb->s_fs_info = kzalloc(sizeof(struct orangefs_sb_info_s), GFP_KERNEL);
  301. if (!ORANGEFS_SB(sb))
  302. return -ENOMEM;
  303. ORANGEFS_SB(sb)->sb = sb;
  304. ORANGEFS_SB(sb)->root_khandle = fs_mount->root_khandle;
  305. ORANGEFS_SB(sb)->fs_id = fs_mount->fs_id;
  306. ORANGEFS_SB(sb)->id = fs_mount->id;
  307. if (data) {
  308. ret = parse_mount_options(sb, data, silent);
  309. if (ret)
  310. return ret;
  311. }
  312. /* Hang the xattr handlers off the superblock */
  313. sb->s_xattr = orangefs_xattr_handlers;
  314. sb->s_magic = ORANGEFS_SUPER_MAGIC;
  315. sb->s_op = &orangefs_s_ops;
  316. sb->s_d_op = &orangefs_dentry_operations;
  317. sb->s_blocksize = orangefs_bufmap_size_query();
  318. sb->s_blocksize_bits = orangefs_bufmap_shift_query();
  319. sb->s_maxbytes = MAX_LFS_FILESIZE;
  320. root_object.khandle = ORANGEFS_SB(sb)->root_khandle;
  321. root_object.fs_id = ORANGEFS_SB(sb)->fs_id;
  322. gossip_debug(GOSSIP_SUPER_DEBUG,
  323. "get inode %pU, fsid %d\n",
  324. &root_object.khandle,
  325. root_object.fs_id);
  326. root = orangefs_iget(sb, &root_object);
  327. if (IS_ERR(root))
  328. return PTR_ERR(root);
  329. gossip_debug(GOSSIP_SUPER_DEBUG,
  330. "Allocated root inode [%p] with mode %x\n",
  331. root,
  332. root->i_mode);
  333. /* allocates and places root dentry in dcache */
  334. root_dentry = d_make_root(root);
  335. if (!root_dentry)
  336. return -ENOMEM;
  337. sb->s_export_op = &orangefs_export_ops;
  338. sb->s_root = root_dentry;
  339. return 0;
  340. }
  341. struct dentry *orangefs_mount(struct file_system_type *fst,
  342. int flags,
  343. const char *devname,
  344. void *data)
  345. {
  346. int ret = -EINVAL;
  347. struct super_block *sb = ERR_PTR(-EINVAL);
  348. struct orangefs_kernel_op_s *new_op;
  349. struct dentry *d = ERR_PTR(-EINVAL);
  350. gossip_debug(GOSSIP_SUPER_DEBUG,
  351. "orangefs_mount: called with devname %s\n",
  352. devname);
  353. if (!devname) {
  354. gossip_err("ERROR: device name not specified.\n");
  355. return ERR_PTR(-EINVAL);
  356. }
  357. new_op = op_alloc(ORANGEFS_VFS_OP_FS_MOUNT);
  358. if (!new_op)
  359. return ERR_PTR(-ENOMEM);
  360. strncpy(new_op->upcall.req.fs_mount.orangefs_config_server,
  361. devname,
  362. ORANGEFS_MAX_SERVER_ADDR_LEN);
  363. gossip_debug(GOSSIP_SUPER_DEBUG,
  364. "Attempting ORANGEFS Mount via host %s\n",
  365. new_op->upcall.req.fs_mount.orangefs_config_server);
  366. ret = service_operation(new_op, "orangefs_mount", 0);
  367. gossip_debug(GOSSIP_SUPER_DEBUG,
  368. "orangefs_mount: mount got return value of %d\n", ret);
  369. if (ret)
  370. goto free_op;
  371. if (new_op->downcall.resp.fs_mount.fs_id == ORANGEFS_FS_ID_NULL) {
  372. gossip_err("ERROR: Retrieved null fs_id\n");
  373. ret = -EINVAL;
  374. goto free_op;
  375. }
  376. sb = sget(fst, NULL, set_anon_super, flags, NULL);
  377. if (IS_ERR(sb)) {
  378. d = ERR_CAST(sb);
  379. goto free_op;
  380. }
  381. ret = orangefs_fill_sb(sb,
  382. &new_op->downcall.resp.fs_mount, data,
  383. flags & MS_SILENT ? 1 : 0);
  384. if (ret) {
  385. d = ERR_PTR(ret);
  386. goto free_op;
  387. }
  388. /*
  389. * on successful mount, store the devname and data
  390. * used
  391. */
  392. strncpy(ORANGEFS_SB(sb)->devname,
  393. devname,
  394. ORANGEFS_MAX_SERVER_ADDR_LEN);
  395. /* mount_pending must be cleared */
  396. ORANGEFS_SB(sb)->mount_pending = 0;
  397. /*
  398. * finally, add this sb to our list of known orangefs
  399. * sb's
  400. */
  401. gossip_debug(GOSSIP_SUPER_DEBUG,
  402. "Adding SB %p to orangefs superblocks\n",
  403. ORANGEFS_SB(sb));
  404. spin_lock(&orangefs_superblocks_lock);
  405. list_add_tail(&ORANGEFS_SB(sb)->list, &orangefs_superblocks);
  406. spin_unlock(&orangefs_superblocks_lock);
  407. op_release(new_op);
  408. return dget(sb->s_root);
  409. free_op:
  410. gossip_err("orangefs_mount: mount request failed with %d\n", ret);
  411. if (ret == -EINVAL) {
  412. gossip_err("Ensure that all orangefs-servers have the same FS configuration files\n");
  413. gossip_err("Look at pvfs2-client-core log file (typically /tmp/pvfs2-client.log) for more details\n");
  414. }
  415. op_release(new_op);
  416. return d;
  417. }
  418. void orangefs_kill_sb(struct super_block *sb)
  419. {
  420. gossip_debug(GOSSIP_SUPER_DEBUG, "orangefs_kill_sb: called\n");
  421. /* provided sb cleanup */
  422. kill_anon_super(sb);
  423. /*
  424. * issue the unmount to userspace to tell it to remove the
  425. * dynamic mount info it has for this superblock
  426. */
  427. orangefs_unmount_sb(sb);
  428. /* remove the sb from our list of orangefs specific sb's */
  429. spin_lock(&orangefs_superblocks_lock);
  430. __list_del_entry(&ORANGEFS_SB(sb)->list); /* not list_del_init */
  431. ORANGEFS_SB(sb)->list.prev = NULL;
  432. spin_unlock(&orangefs_superblocks_lock);
  433. /*
  434. * make sure that ORANGEFS_DEV_REMOUNT_ALL loop that might've seen us
  435. * gets completed before we free the dang thing.
  436. */
  437. mutex_lock(&request_mutex);
  438. mutex_unlock(&request_mutex);
  439. /* free the orangefs superblock private data */
  440. kfree(ORANGEFS_SB(sb));
  441. }
  442. int orangefs_inode_cache_initialize(void)
  443. {
  444. orangefs_inode_cache = kmem_cache_create("orangefs_inode_cache",
  445. sizeof(struct orangefs_inode_s),
  446. 0,
  447. ORANGEFS_CACHE_CREATE_FLAGS,
  448. orangefs_inode_cache_ctor);
  449. if (!orangefs_inode_cache) {
  450. gossip_err("Cannot create orangefs_inode_cache\n");
  451. return -ENOMEM;
  452. }
  453. return 0;
  454. }
  455. int orangefs_inode_cache_finalize(void)
  456. {
  457. kmem_cache_destroy(orangefs_inode_cache);
  458. return 0;
  459. }