lock.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net>
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/backing-dev.h>
  17. #include <linux/fs.h>
  18. #include <linux/fsnotify.h>
  19. #include <linux/mempool.h>
  20. #include "netfs.h"
  21. static int pohmelfs_send_lock_trans(struct pohmelfs_inode *pi,
  22. u64 id, u64 start, u32 size, int type)
  23. {
  24. struct inode *inode = &pi->vfs_inode;
  25. struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb);
  26. struct netfs_trans *t;
  27. struct netfs_cmd *cmd;
  28. int path_len, err;
  29. void *data;
  30. struct netfs_lock *l;
  31. int isize = (type & POHMELFS_LOCK_GRAB) ? 0 : sizeof(struct netfs_inode_info);
  32. err = pohmelfs_path_length(pi);
  33. if (err < 0)
  34. goto err_out_exit;
  35. path_len = err;
  36. err = -ENOMEM;
  37. t = netfs_trans_alloc(psb, path_len + sizeof(struct netfs_lock) + isize,
  38. NETFS_TRANS_SINGLE_DST, 0);
  39. if (!t)
  40. goto err_out_exit;
  41. cmd = netfs_trans_current(t);
  42. data = cmd + 1;
  43. err = pohmelfs_construct_path_string(pi, data, path_len);
  44. if (err < 0)
  45. goto err_out_free;
  46. path_len = err;
  47. l = data + path_len;
  48. l->start = start;
  49. l->size = size;
  50. l->type = type;
  51. l->ino = pi->ino;
  52. cmd->cmd = NETFS_LOCK;
  53. cmd->start = 0;
  54. cmd->id = id;
  55. cmd->size = sizeof(struct netfs_lock) + path_len + isize;
  56. cmd->ext = path_len;
  57. cmd->csize = 0;
  58. netfs_convert_cmd(cmd);
  59. netfs_convert_lock(l);
  60. if (isize) {
  61. struct netfs_inode_info *info = (struct netfs_inode_info *)(l + 1);
  62. info->mode = inode->i_mode;
  63. info->nlink = inode->i_nlink;
  64. info->uid = inode->i_uid;
  65. info->gid = inode->i_gid;
  66. info->blocks = inode->i_blocks;
  67. info->rdev = inode->i_rdev;
  68. info->size = inode->i_size;
  69. info->version = inode->i_version;
  70. netfs_convert_inode_info(info);
  71. }
  72. netfs_trans_update(cmd, t, path_len + sizeof(struct netfs_lock) + isize);
  73. return netfs_trans_finish(t, psb);
  74. err_out_free:
  75. netfs_trans_free(t);
  76. err_out_exit:
  77. printk("%s: err: %d.\n", __func__, err);
  78. return err;
  79. }
  80. int pohmelfs_data_lock(struct pohmelfs_inode *pi, u64 start, u32 size, int type)
  81. {
  82. struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb);
  83. struct pohmelfs_mcache *m;
  84. int err = -ENOMEM;
  85. struct iattr iattr;
  86. struct inode *inode = &pi->vfs_inode;
  87. dprintk("%s: %p: ino: %llu, start: %llu, size: %u, "
  88. "type: %d, locked as: %d, owned: %d.\n",
  89. __func__, &pi->vfs_inode, pi->ino,
  90. start, size, type, pi->lock_type,
  91. !!test_bit(NETFS_INODE_OWNED, &pi->state));
  92. if (!pohmelfs_need_lock(pi, type))
  93. return 0;
  94. m = pohmelfs_mcache_alloc(psb, start, size, NULL);
  95. if (IS_ERR(m))
  96. return PTR_ERR(m);
  97. err = pohmelfs_send_lock_trans(pi, m->gen, start, size,
  98. type | POHMELFS_LOCK_GRAB);
  99. if (err)
  100. goto err_out_put;
  101. err = wait_for_completion_timeout(&m->complete, psb->mcache_timeout);
  102. if (err)
  103. err = m->err;
  104. else
  105. err = -ETIMEDOUT;
  106. if (err) {
  107. printk("%s: %p: ino: %llu, mgen: %llu, start: %llu, size: %u, err: %d.\n",
  108. __func__, &pi->vfs_inode, pi->ino, m->gen, start, size, err);
  109. }
  110. if (err && (err != -ENOENT))
  111. goto err_out_put;
  112. if (!err) {
  113. netfs_convert_inode_info(&m->info);
  114. iattr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_SIZE | ATTR_ATIME;
  115. iattr.ia_mode = m->info.mode;
  116. iattr.ia_uid = m->info.uid;
  117. iattr.ia_gid = m->info.gid;
  118. iattr.ia_size = m->info.size;
  119. iattr.ia_atime = CURRENT_TIME;
  120. dprintk("%s: %p: ino: %llu, mgen: %llu, start: %llu, isize: %llu -> %llu.\n",
  121. __func__, &pi->vfs_inode, pi->ino, m->gen, start, inode->i_size, m->info.size);
  122. err = pohmelfs_setattr_raw(inode, &iattr);
  123. if (!err) {
  124. struct dentry *dentry = d_find_alias(inode);
  125. if (dentry) {
  126. fsnotify_change(dentry, iattr.ia_valid);
  127. dput(dentry);
  128. }
  129. }
  130. }
  131. pi->lock_type = type;
  132. set_bit(NETFS_INODE_OWNED, &pi->state);
  133. pohmelfs_mcache_put(psb, m);
  134. return 0;
  135. err_out_put:
  136. pohmelfs_mcache_put(psb, m);
  137. return err;
  138. }
  139. int pohmelfs_data_unlock(struct pohmelfs_inode *pi, u64 start, u32 size, int type)
  140. {
  141. dprintk("%s: %p: ino: %llu, start: %llu, size: %u, type: %d.\n",
  142. __func__, &pi->vfs_inode, pi->ino, start, size, type);
  143. pi->lock_type = 0;
  144. clear_bit(NETFS_INODE_REMOTE_DIR_SYNCED, &pi->state);
  145. clear_bit(NETFS_INODE_OWNED, &pi->state);
  146. return pohmelfs_send_lock_trans(pi, pi->ino, start, size, type);
  147. }