/samba-3.5.6/source3/modules/vfs_syncops.c

https://github.com/theuni/XBMC-deps · C · 228 lines · 144 code · 27 blank · 57 comment · 25 complexity · b77d0db5e6b20474f44ca5ec2d875789 MD5 · raw file

  1. /*
  2. * ensure meta data operations are performed synchronously
  3. *
  4. * Copyright (C) Andrew Tridgell 2007
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #include "includes.h"
  21. /*
  22. Some filesystems (even some journaled filesystems) require that a
  23. fsync() be performed on many meta data operations to ensure that the
  24. operation is guaranteed to remain in the filesystem after a power
  25. failure. This is particularly important for some cluster filesystems
  26. which are participating in a node failover system with clustered
  27. Samba
  28. On those filesystems this module provides a way to perform those
  29. operations safely.
  30. */
  31. /*
  32. most of the performance loss with this module is in fsync on close().
  33. You can disable that with syncops:onclose = no
  34. */
  35. static bool sync_onclose;
  36. /*
  37. given a filename, find the parent directory
  38. */
  39. static char *parent_dir(TALLOC_CTX *mem_ctx, const char *name)
  40. {
  41. const char *p = strrchr(name, '/');
  42. if (p == NULL) {
  43. return talloc_strdup(mem_ctx, ".");
  44. }
  45. return talloc_strndup(mem_ctx, name, (p+1) - name);
  46. }
  47. /*
  48. fsync a directory by name
  49. */
  50. static void syncops_sync_directory(const char *dname)
  51. {
  52. #ifdef O_DIRECTORY
  53. int fd = open(dname, O_DIRECTORY|O_RDONLY);
  54. if (fd != -1) {
  55. fsync(fd);
  56. close(fd);
  57. }
  58. #else
  59. DIR *d = opendir(dname);
  60. if (d != NULL) {
  61. fsync(dirfd(d));
  62. closedir(d);
  63. }
  64. #endif
  65. }
  66. /*
  67. sync two meta data changes for 2 names
  68. */
  69. static void syncops_two_names(const char *name1, const char *name2)
  70. {
  71. TALLOC_CTX *tmp_ctx = talloc_new(NULL);
  72. char *parent1, *parent2;
  73. parent1 = parent_dir(tmp_ctx, name1);
  74. parent2 = parent_dir(tmp_ctx, name2);
  75. if (!parent1 || !parent2) {
  76. talloc_free(tmp_ctx);
  77. return;
  78. }
  79. syncops_sync_directory(parent1);
  80. if (strcmp(parent1, parent2) != 0) {
  81. syncops_sync_directory(parent2);
  82. }
  83. talloc_free(tmp_ctx);
  84. }
  85. /*
  86. sync two meta data changes for 1 names
  87. */
  88. static void syncops_name(const char *name)
  89. {
  90. char *parent;
  91. parent = parent_dir(NULL, name);
  92. if (parent) {
  93. syncops_sync_directory(parent);
  94. talloc_free(parent);
  95. }
  96. }
  97. /*
  98. sync two meta data changes for 1 names
  99. */
  100. static void syncops_smb_fname(const struct smb_filename *smb_fname)
  101. {
  102. char *parent;
  103. parent = parent_dir(NULL, smb_fname->base_name);
  104. if (parent) {
  105. syncops_sync_directory(parent);
  106. talloc_free(parent);
  107. }
  108. }
  109. /*
  110. rename needs special handling, as we may need to fsync two directories
  111. */
  112. static int syncops_rename(vfs_handle_struct *handle,
  113. const struct smb_filename *smb_fname_src,
  114. const struct smb_filename *smb_fname_dst)
  115. {
  116. int ret = SMB_VFS_NEXT_RENAME(handle, smb_fname_src, smb_fname_dst);
  117. if (ret == 0) {
  118. syncops_two_names(smb_fname_src->base_name,
  119. smb_fname_dst->base_name);
  120. }
  121. return ret;
  122. }
  123. /* handle the rest with a macro */
  124. #define SYNCOPS_NEXT(op, fname, args) do { \
  125. int ret = SMB_VFS_NEXT_ ## op args; \
  126. if (ret == 0 && fname) syncops_name(fname); \
  127. return ret; \
  128. } while (0)
  129. #define SYNCOPS_NEXT_SMB_FNAME(op, fname, args) do { \
  130. int ret = SMB_VFS_NEXT_ ## op args; \
  131. if (ret == 0 && fname) syncops_smb_fname(fname); \
  132. return ret; \
  133. } while (0)
  134. static int syncops_symlink(vfs_handle_struct *handle,
  135. const char *oldname, const char *newname)
  136. {
  137. SYNCOPS_NEXT(SYMLINK, newname, (handle, oldname, newname));
  138. }
  139. static int syncops_link(vfs_handle_struct *handle,
  140. const char *oldname, const char *newname)
  141. {
  142. SYNCOPS_NEXT(LINK, newname, (handle, oldname, newname));
  143. }
  144. static int syncops_open(vfs_handle_struct *handle,
  145. struct smb_filename *smb_fname, files_struct *fsp,
  146. int flags, mode_t mode)
  147. {
  148. SYNCOPS_NEXT_SMB_FNAME(OPEN, (flags&O_CREAT?smb_fname:NULL),
  149. (handle, smb_fname, fsp, flags, mode));
  150. }
  151. static int syncops_unlink(vfs_handle_struct *handle,
  152. const struct smb_filename *smb_fname)
  153. {
  154. SYNCOPS_NEXT_SMB_FNAME(UNLINK, smb_fname, (handle, smb_fname));
  155. }
  156. static int syncops_mknod(vfs_handle_struct *handle,
  157. const char *fname, mode_t mode, SMB_DEV_T dev)
  158. {
  159. SYNCOPS_NEXT(MKNOD, fname, (handle, fname, mode, dev));
  160. }
  161. static int syncops_mkdir(vfs_handle_struct *handle, const char *fname, mode_t mode)
  162. {
  163. SYNCOPS_NEXT(MKDIR, fname, (handle, fname, mode));
  164. }
  165. static int syncops_rmdir(vfs_handle_struct *handle, const char *fname)
  166. {
  167. SYNCOPS_NEXT(RMDIR, fname, (handle, fname));
  168. }
  169. /* close needs to be handled specially */
  170. static int syncops_close(vfs_handle_struct *handle, files_struct *fsp)
  171. {
  172. if (fsp->can_write && sync_onclose) {
  173. /* ideally we'd only do this if we have written some
  174. data, but there is no flag for that in fsp yet. */
  175. fsync(fsp->fh->fd);
  176. }
  177. return SMB_VFS_NEXT_CLOSE(handle, fsp);
  178. }
  179. static struct vfs_fn_pointers vfs_syncops_fns = {
  180. .mkdir = syncops_mkdir,
  181. .rmdir = syncops_rmdir,
  182. .open = syncops_open,
  183. .rename = syncops_rename,
  184. .unlink = syncops_unlink,
  185. .symlink = syncops_symlink,
  186. .link = syncops_link,
  187. .mknod = syncops_mknod,
  188. .close_fn = syncops_close,
  189. };
  190. NTSTATUS vfs_syncops_init(void)
  191. {
  192. NTSTATUS ret;
  193. ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "syncops",
  194. &vfs_syncops_fns);
  195. if (!NT_STATUS_IS_OK(ret))
  196. return ret;
  197. sync_onclose = lp_parm_bool(-1, "syncops", "onclose", true);
  198. return ret;
  199. }