052-03-ubifs-Implement-RENAME_EXCHANGE.patch 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. From: Richard Weinberger <richard@nod.at>
  2. Date: Tue, 13 Sep 2016 16:18:57 +0200
  3. Subject: [PATCH] ubifs: Implement RENAME_EXCHANGE
  4. Adds RENAME_EXCHANGE to UBIFS, the operation itself
  5. is completely disjunct from a regular rename() that's
  6. why we dispatch very early in ubifs_reaname().
  7. RENAME_EXCHANGE used by the renameat2() system call
  8. allows the caller to exchange two paths atomically.
  9. Both paths have to exist and have to be on the same
  10. filesystem.
  11. Signed-off-by: Richard Weinberger <richard@nod.at>
  12. ---
  13. --- a/fs/ubifs/dir.c
  14. +++ b/fs/ubifs/dir.c
  15. @@ -1101,11 +1101,6 @@ static int ubifs_rename(struct inode *ol
  16. old_dentry, old_inode->i_ino, old_dir->i_ino,
  17. new_dentry, new_dir->i_ino, flags);
  18. - if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT))
  19. - return -EINVAL;
  20. -
  21. - ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
  22. - ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
  23. if (unlink)
  24. ubifs_assert(mutex_is_locked(&new_inode->i_mutex));
  25. @@ -1290,6 +1285,64 @@ out_cancel:
  26. return err;
  27. }
  28. +static int ubifs_xrename(struct inode *old_dir, struct dentry *old_dentry,
  29. + struct inode *new_dir, struct dentry *new_dentry)
  30. +{
  31. + struct ubifs_info *c = old_dir->i_sb->s_fs_info;
  32. + struct ubifs_budget_req req = { .new_dent = 1, .mod_dent = 1,
  33. + .dirtied_ino = 2 };
  34. + int sync = IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir);
  35. + struct inode *fst_inode = d_inode(old_dentry);
  36. + struct inode *snd_inode = d_inode(new_dentry);
  37. + struct timespec time;
  38. + int err;
  39. +
  40. + ubifs_assert(fst_inode && snd_inode);
  41. +
  42. + lock_4_inodes(old_dir, new_dir, NULL, NULL);
  43. +
  44. + time = ubifs_current_time(old_dir);
  45. + fst_inode->i_ctime = time;
  46. + snd_inode->i_ctime = time;
  47. + old_dir->i_mtime = old_dir->i_ctime = time;
  48. + new_dir->i_mtime = new_dir->i_ctime = time;
  49. +
  50. + if (old_dir != new_dir) {
  51. + if (S_ISDIR(fst_inode->i_mode) && !S_ISDIR(snd_inode->i_mode)) {
  52. + inc_nlink(new_dir);
  53. + drop_nlink(old_dir);
  54. + }
  55. + else if (!S_ISDIR(fst_inode->i_mode) && S_ISDIR(snd_inode->i_mode)) {
  56. + drop_nlink(new_dir);
  57. + inc_nlink(old_dir);
  58. + }
  59. + }
  60. +
  61. + err = ubifs_jnl_xrename(c, old_dir, old_dentry, new_dir, new_dentry,
  62. + sync);
  63. +
  64. + unlock_4_inodes(old_dir, new_dir, NULL, NULL);
  65. + ubifs_release_budget(c, &req);
  66. +
  67. + return err;
  68. +}
  69. +
  70. +static int ubifs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  71. + struct inode *new_dir, struct dentry *new_dentry,
  72. + unsigned int flags)
  73. +{
  74. + if (flags & ~(RENAME_NOREPLACE | RENAME_WHITEOUT | RENAME_EXCHANGE))
  75. + return -EINVAL;
  76. +
  77. + ubifs_assert(mutex_is_locked(&old_dir->i_mutex));
  78. + ubifs_assert(mutex_is_locked(&new_dir->i_mutex));
  79. +
  80. + if (flags & RENAME_EXCHANGE)
  81. + return ubifs_xrename(old_dir, old_dentry, new_dir, new_dentry);
  82. +
  83. + return ubifs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
  84. +}
  85. +
  86. int ubifs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  87. struct kstat *stat)
  88. {
  89. @@ -1338,7 +1391,7 @@ const struct inode_operations ubifs_dir_
  90. .mkdir = ubifs_mkdir,
  91. .rmdir = ubifs_rmdir,
  92. .mknod = ubifs_mknod,
  93. - .rename2 = ubifs_rename,
  94. + .rename2 = ubifs_rename2,
  95. .setattr = ubifs_setattr,
  96. .getattr = ubifs_getattr,
  97. .setxattr = ubifs_setxattr,
  98. --- a/fs/ubifs/journal.c
  99. +++ b/fs/ubifs/journal.c
  100. @@ -908,6 +908,147 @@ int ubifs_jnl_delete_inode(struct ubifs_
  101. }
  102. /**
  103. + * ubifs_jnl_xrename - cross rename two directory entries.
  104. + * @c: UBIFS file-system description object
  105. + * @fst_dir: parent inode of 1st directory entry to exchange
  106. + * @fst_dentry: 1st directory entry to exchange
  107. + * @snd_dir: parent inode of 2nd directory entry to exchange
  108. + * @snd_dentry: 2nd directory entry to exchange
  109. + * @sync: non-zero if the write-buffer has to be synchronized
  110. + *
  111. + * This function implements the cross rename operation which may involve
  112. + * writing 2 inodes and 2 directory entries. It marks the written inodes as clean
  113. + * and returns zero on success. In case of failure, a negative error code is
  114. + * returned.
  115. + */
  116. +int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
  117. + const struct dentry *fst_dentry,
  118. + const struct inode *snd_dir,
  119. + const struct dentry *snd_dentry, int sync)
  120. +{
  121. + union ubifs_key key;
  122. + struct ubifs_dent_node *dent1, *dent2;
  123. + int err, dlen1, dlen2, lnum, offs, len, plen = UBIFS_INO_NODE_SZ;
  124. + int aligned_dlen1, aligned_dlen2;
  125. + int twoparents = (fst_dir != snd_dir);
  126. + const struct inode *fst_inode = d_inode(fst_dentry);
  127. + const struct inode *snd_inode = d_inode(snd_dentry);
  128. + void *p;
  129. +
  130. + dbg_jnl("dent '%pd' in dir ino %lu between dent '%pd' in dir ino %lu",
  131. + fst_dentry, fst_dir->i_ino, snd_dentry, snd_dir->i_ino);
  132. +
  133. + ubifs_assert(ubifs_inode(fst_dir)->data_len == 0);
  134. + ubifs_assert(ubifs_inode(snd_dir)->data_len == 0);
  135. + ubifs_assert(mutex_is_locked(&ubifs_inode(fst_dir)->ui_mutex));
  136. + ubifs_assert(mutex_is_locked(&ubifs_inode(snd_dir)->ui_mutex));
  137. +
  138. + dlen1 = UBIFS_DENT_NODE_SZ + snd_dentry->d_name.len + 1;
  139. + dlen2 = UBIFS_DENT_NODE_SZ + fst_dentry->d_name.len + 1;
  140. + aligned_dlen1 = ALIGN(dlen1, 8);
  141. + aligned_dlen2 = ALIGN(dlen2, 8);
  142. +
  143. + len = aligned_dlen1 + aligned_dlen2 + ALIGN(plen, 8);
  144. + if (twoparents)
  145. + len += plen;
  146. +
  147. + dent1 = kmalloc(len, GFP_NOFS);
  148. + if (!dent1)
  149. + return -ENOMEM;
  150. +
  151. + /* Make reservation before allocating sequence numbers */
  152. + err = make_reservation(c, BASEHD, len);
  153. + if (err)
  154. + goto out_free;
  155. +
  156. + /* Make new dent for 1st entry */
  157. + dent1->ch.node_type = UBIFS_DENT_NODE;
  158. + dent_key_init_flash(c, &dent1->key, snd_dir->i_ino, &snd_dentry->d_name);
  159. + dent1->inum = cpu_to_le64(fst_inode->i_ino);
  160. + dent1->type = get_dent_type(fst_inode->i_mode);
  161. + dent1->nlen = cpu_to_le16(snd_dentry->d_name.len);
  162. + memcpy(dent1->name, snd_dentry->d_name.name, snd_dentry->d_name.len);
  163. + dent1->name[snd_dentry->d_name.len] = '\0';
  164. + zero_dent_node_unused(dent1);
  165. + ubifs_prep_grp_node(c, dent1, dlen1, 0);
  166. +
  167. + /* Make new dent for 2nd entry */
  168. + dent2 = (void *)dent1 + aligned_dlen1;
  169. + dent2->ch.node_type = UBIFS_DENT_NODE;
  170. + dent_key_init_flash(c, &dent2->key, fst_dir->i_ino, &fst_dentry->d_name);
  171. + dent2->inum = cpu_to_le64(snd_inode->i_ino);
  172. + dent2->type = get_dent_type(snd_inode->i_mode);
  173. + dent2->nlen = cpu_to_le16(fst_dentry->d_name.len);
  174. + memcpy(dent2->name, fst_dentry->d_name.name, fst_dentry->d_name.len);
  175. + dent2->name[fst_dentry->d_name.len] = '\0';
  176. + zero_dent_node_unused(dent2);
  177. + ubifs_prep_grp_node(c, dent2, dlen2, 0);
  178. +
  179. + p = (void *)dent2 + aligned_dlen2;
  180. + if (!twoparents)
  181. + pack_inode(c, p, fst_dir, 1);
  182. + else {
  183. + pack_inode(c, p, fst_dir, 0);
  184. + p += ALIGN(plen, 8);
  185. + pack_inode(c, p, snd_dir, 1);
  186. + }
  187. +
  188. + err = write_head(c, BASEHD, dent1, len, &lnum, &offs, sync);
  189. + if (err)
  190. + goto out_release;
  191. + if (!sync) {
  192. + struct ubifs_wbuf *wbuf = &c->jheads[BASEHD].wbuf;
  193. +
  194. + ubifs_wbuf_add_ino_nolock(wbuf, fst_dir->i_ino);
  195. + ubifs_wbuf_add_ino_nolock(wbuf, snd_dir->i_ino);
  196. + }
  197. + release_head(c, BASEHD);
  198. +
  199. + dent_key_init(c, &key, snd_dir->i_ino, &snd_dentry->d_name);
  200. + err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen1, &snd_dentry->d_name);
  201. + if (err)
  202. + goto out_ro;
  203. +
  204. + offs += aligned_dlen1;
  205. + dent_key_init(c, &key, fst_dir->i_ino, &fst_dentry->d_name);
  206. + err = ubifs_tnc_add_nm(c, &key, lnum, offs, dlen2, &fst_dentry->d_name);
  207. + if (err)
  208. + goto out_ro;
  209. +
  210. + offs += aligned_dlen2;
  211. +
  212. + ino_key_init(c, &key, fst_dir->i_ino);
  213. + err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  214. + if (err)
  215. + goto out_ro;
  216. +
  217. + if (twoparents) {
  218. + offs += ALIGN(plen, 8);
  219. + ino_key_init(c, &key, snd_dir->i_ino);
  220. + err = ubifs_tnc_add(c, &key, lnum, offs, plen);
  221. + if (err)
  222. + goto out_ro;
  223. + }
  224. +
  225. + finish_reservation(c);
  226. +
  227. + mark_inode_clean(c, ubifs_inode(fst_dir));
  228. + if (twoparents)
  229. + mark_inode_clean(c, ubifs_inode(snd_dir));
  230. + kfree(dent1);
  231. + return 0;
  232. +
  233. +out_release:
  234. + release_head(c, BASEHD);
  235. +out_ro:
  236. + ubifs_ro_mode(c, err);
  237. + finish_reservation(c);
  238. +out_free:
  239. + kfree(dent1);
  240. + return err;
  241. +}
  242. +
  243. +/**
  244. * ubifs_jnl_rename - rename a directory entry.
  245. * @c: UBIFS file-system description object
  246. * @old_dir: parent inode of directory entry to rename
  247. --- a/fs/ubifs/ubifs.h
  248. +++ b/fs/ubifs/ubifs.h
  249. @@ -1544,6 +1544,10 @@ int ubifs_jnl_write_data(struct ubifs_in
  250. const union ubifs_key *key, const void *buf, int len);
  251. int ubifs_jnl_write_inode(struct ubifs_info *c, const struct inode *inode);
  252. int ubifs_jnl_delete_inode(struct ubifs_info *c, const struct inode *inode);
  253. +int ubifs_jnl_xrename(struct ubifs_info *c, const struct inode *fst_dir,
  254. + const struct dentry *fst_dentry,
  255. + const struct inode *snd_dir,
  256. + const struct dentry *snd_dentry, int sync);
  257. int ubifs_jnl_rename(struct ubifs_info *c, const struct inode *old_dir,
  258. const struct dentry *old_dentry,
  259. const struct inode *new_dir,