Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005, 2006 |
Boaz Harrosh | 27d2e14 | 2009-06-14 17:23:09 +0300 | [diff] [blame] | 3 | * Avishay Traeger (avishay@gmail.com) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 4 | * Copyright (C) 2008, 2009 |
Boaz Harrosh | aa281ac | 2014-10-19 19:38:58 +0300 | [diff] [blame] | 5 | * Boaz Harrosh <ooo@electrozaur.com> |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyrights for code taken from ext2: |
| 8 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 9 | * Remy Card (card@masi.ibp.fr) |
| 10 | * Laboratoire MASI - Institut Blaise Pascal |
| 11 | * Universite Pierre et Marie Curie (Paris VI) |
| 12 | * from |
| 13 | * linux/fs/minix/inode.c |
| 14 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 15 | * |
| 16 | * This file is part of exofs. |
| 17 | * |
| 18 | * exofs is free software; you can redistribute it and/or modify |
| 19 | * it under the terms of the GNU General Public License as published by |
| 20 | * the Free Software Foundation. Since it is based on ext2, and the only |
| 21 | * valid version of GPL for the Linux kernel is version 2, the only valid |
| 22 | * version of GPL for exofs is version 2. |
| 23 | * |
| 24 | * exofs is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with exofs; if not, write to the Free Software |
| 31 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 32 | */ |
| 33 | |
| 34 | #include "exofs.h" |
| 35 | |
| 36 | static inline int exofs_add_nondir(struct dentry *dentry, struct inode *inode) |
| 37 | { |
| 38 | int err = exofs_add_link(dentry, inode); |
| 39 | if (!err) { |
| 40 | d_instantiate(dentry, inode); |
| 41 | return 0; |
| 42 | } |
| 43 | inode_dec_link_count(inode); |
| 44 | iput(inode); |
| 45 | return err; |
| 46 | } |
| 47 | |
| 48 | static struct dentry *exofs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 49 | unsigned int flags) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 50 | { |
| 51 | struct inode *inode; |
| 52 | ino_t ino; |
| 53 | |
| 54 | if (dentry->d_name.len > EXOFS_NAME_LEN) |
| 55 | return ERR_PTR(-ENAMETOOLONG); |
| 56 | |
| 57 | ino = exofs_inode_by_name(dir, dentry); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 58 | inode = ino ? exofs_iget(dir->i_sb, ino) : NULL; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 59 | return d_splice_alias(inode, dentry); |
| 60 | } |
| 61 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 62 | static int exofs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 63 | bool excl) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 64 | { |
| 65 | struct inode *inode = exofs_new_inode(dir, mode); |
| 66 | int err = PTR_ERR(inode); |
| 67 | if (!IS_ERR(inode)) { |
| 68 | inode->i_op = &exofs_file_inode_operations; |
| 69 | inode->i_fop = &exofs_file_operations; |
| 70 | inode->i_mapping->a_ops = &exofs_aops; |
| 71 | mark_inode_dirty(inode); |
| 72 | err = exofs_add_nondir(dentry, inode); |
| 73 | } |
| 74 | return err; |
| 75 | } |
| 76 | |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 77 | static int exofs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 78 | dev_t rdev) |
| 79 | { |
| 80 | struct inode *inode; |
| 81 | int err; |
| 82 | |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 83 | inode = exofs_new_inode(dir, mode); |
| 84 | err = PTR_ERR(inode); |
| 85 | if (!IS_ERR(inode)) { |
| 86 | init_special_inode(inode, inode->i_mode, rdev); |
| 87 | mark_inode_dirty(inode); |
| 88 | err = exofs_add_nondir(dentry, inode); |
| 89 | } |
| 90 | return err; |
| 91 | } |
| 92 | |
| 93 | static int exofs_symlink(struct inode *dir, struct dentry *dentry, |
| 94 | const char *symname) |
| 95 | { |
| 96 | struct super_block *sb = dir->i_sb; |
| 97 | int err = -ENAMETOOLONG; |
| 98 | unsigned l = strlen(symname)+1; |
| 99 | struct inode *inode; |
| 100 | struct exofs_i_info *oi; |
| 101 | |
| 102 | if (l > sb->s_blocksize) |
| 103 | goto out; |
| 104 | |
| 105 | inode = exofs_new_inode(dir, S_IFLNK | S_IRWXUGO); |
| 106 | err = PTR_ERR(inode); |
| 107 | if (IS_ERR(inode)) |
| 108 | goto out; |
| 109 | |
| 110 | oi = exofs_i(inode); |
| 111 | if (l > sizeof(oi->i_data)) { |
| 112 | /* slow symlink */ |
Al Viro | a5ef103 | 2015-05-02 10:46:42 -0400 | [diff] [blame] | 113 | inode->i_op = &page_symlink_inode_operations; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 114 | inode->i_mapping->a_ops = &exofs_aops; |
| 115 | memset(oi->i_data, 0, sizeof(oi->i_data)); |
| 116 | |
| 117 | err = page_symlink(inode, symname, l); |
| 118 | if (err) |
| 119 | goto out_fail; |
| 120 | } else { |
| 121 | /* fast symlink */ |
Al Viro | a5ef103 | 2015-05-02 10:46:42 -0400 | [diff] [blame] | 122 | inode->i_op = &simple_symlink_inode_operations; |
| 123 | inode->i_link = (char *)oi->i_data; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 124 | memcpy(oi->i_data, symname, l); |
| 125 | inode->i_size = l-1; |
| 126 | } |
| 127 | mark_inode_dirty(inode); |
| 128 | |
| 129 | err = exofs_add_nondir(dentry, inode); |
| 130 | out: |
| 131 | return err; |
| 132 | |
| 133 | out_fail: |
| 134 | inode_dec_link_count(inode); |
| 135 | iput(inode); |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | static int exofs_link(struct dentry *old_dentry, struct inode *dir, |
| 140 | struct dentry *dentry) |
| 141 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 142 | struct inode *inode = d_inode(old_dentry); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 143 | |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 144 | inode->i_ctime = CURRENT_TIME; |
| 145 | inode_inc_link_count(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 146 | ihold(inode); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 147 | |
| 148 | return exofs_add_nondir(dentry, inode); |
| 149 | } |
| 150 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 151 | static int exofs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 152 | { |
| 153 | struct inode *inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 154 | int err; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 155 | |
| 156 | inode_inc_link_count(dir); |
| 157 | |
| 158 | inode = exofs_new_inode(dir, S_IFDIR | mode); |
| 159 | err = PTR_ERR(inode); |
| 160 | if (IS_ERR(inode)) |
| 161 | goto out_dir; |
| 162 | |
| 163 | inode->i_op = &exofs_dir_inode_operations; |
| 164 | inode->i_fop = &exofs_dir_operations; |
| 165 | inode->i_mapping->a_ops = &exofs_aops; |
| 166 | |
| 167 | inode_inc_link_count(inode); |
| 168 | |
| 169 | err = exofs_make_empty(inode, dir); |
| 170 | if (err) |
| 171 | goto out_fail; |
| 172 | |
| 173 | err = exofs_add_link(dentry, inode); |
| 174 | if (err) |
| 175 | goto out_fail; |
| 176 | |
| 177 | d_instantiate(dentry, inode); |
| 178 | out: |
| 179 | return err; |
| 180 | |
| 181 | out_fail: |
| 182 | inode_dec_link_count(inode); |
| 183 | inode_dec_link_count(inode); |
| 184 | iput(inode); |
| 185 | out_dir: |
| 186 | inode_dec_link_count(dir); |
| 187 | goto out; |
| 188 | } |
| 189 | |
| 190 | static int exofs_unlink(struct inode *dir, struct dentry *dentry) |
| 191 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 192 | struct inode *inode = d_inode(dentry); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 193 | struct exofs_dir_entry *de; |
| 194 | struct page *page; |
| 195 | int err = -ENOENT; |
| 196 | |
| 197 | de = exofs_find_entry(dir, dentry, &page); |
| 198 | if (!de) |
| 199 | goto out; |
| 200 | |
| 201 | err = exofs_delete_entry(de, page); |
| 202 | if (err) |
| 203 | goto out; |
| 204 | |
| 205 | inode->i_ctime = dir->i_ctime; |
| 206 | inode_dec_link_count(inode); |
| 207 | err = 0; |
| 208 | out: |
| 209 | return err; |
| 210 | } |
| 211 | |
| 212 | static int exofs_rmdir(struct inode *dir, struct dentry *dentry) |
| 213 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 214 | struct inode *inode = d_inode(dentry); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 215 | int err = -ENOTEMPTY; |
| 216 | |
| 217 | if (exofs_empty_dir(inode)) { |
| 218 | err = exofs_unlink(dir, dentry); |
| 219 | if (!err) { |
| 220 | inode->i_size = 0; |
| 221 | inode_dec_link_count(inode); |
| 222 | inode_dec_link_count(dir); |
| 223 | } |
| 224 | } |
| 225 | return err; |
| 226 | } |
| 227 | |
| 228 | static int exofs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 229 | struct inode *new_dir, struct dentry *new_dentry) |
| 230 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 231 | struct inode *old_inode = d_inode(old_dentry); |
| 232 | struct inode *new_inode = d_inode(new_dentry); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 233 | struct page *dir_page = NULL; |
| 234 | struct exofs_dir_entry *dir_de = NULL; |
| 235 | struct page *old_page; |
| 236 | struct exofs_dir_entry *old_de; |
| 237 | int err = -ENOENT; |
| 238 | |
| 239 | old_de = exofs_find_entry(old_dir, old_dentry, &old_page); |
| 240 | if (!old_de) |
| 241 | goto out; |
| 242 | |
| 243 | if (S_ISDIR(old_inode->i_mode)) { |
| 244 | err = -EIO; |
| 245 | dir_de = exofs_dotdot(old_inode, &dir_page); |
| 246 | if (!dir_de) |
| 247 | goto out_old; |
| 248 | } |
| 249 | |
| 250 | if (new_inode) { |
| 251 | struct page *new_page; |
| 252 | struct exofs_dir_entry *new_de; |
| 253 | |
| 254 | err = -ENOTEMPTY; |
| 255 | if (dir_de && !exofs_empty_dir(new_inode)) |
| 256 | goto out_dir; |
| 257 | |
| 258 | err = -ENOENT; |
| 259 | new_de = exofs_find_entry(new_dir, new_dentry, &new_page); |
| 260 | if (!new_de) |
| 261 | goto out_dir; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 262 | err = exofs_set_link(new_dir, new_de, new_page, old_inode); |
| 263 | new_inode->i_ctime = CURRENT_TIME; |
| 264 | if (dir_de) |
| 265 | drop_nlink(new_inode); |
| 266 | inode_dec_link_count(new_inode); |
| 267 | if (err) |
| 268 | goto out_dir; |
| 269 | } else { |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 270 | err = exofs_add_link(new_dentry, old_inode); |
Al Viro | babfe56 | 2011-03-02 16:42:38 -0500 | [diff] [blame] | 271 | if (err) |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 272 | goto out_dir; |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 273 | if (dir_de) |
| 274 | inode_inc_link_count(new_dir); |
| 275 | } |
| 276 | |
| 277 | old_inode->i_ctime = CURRENT_TIME; |
| 278 | |
| 279 | exofs_delete_entry(old_de, old_page); |
Al Viro | babfe56 | 2011-03-02 16:42:38 -0500 | [diff] [blame] | 280 | mark_inode_dirty(old_inode); |
Boaz Harrosh | e6af00f | 2008-10-28 15:38:12 +0200 | [diff] [blame] | 281 | |
| 282 | if (dir_de) { |
| 283 | err = exofs_set_link(old_inode, dir_de, dir_page, new_dir); |
| 284 | inode_dec_link_count(old_dir); |
| 285 | if (err) |
| 286 | goto out_dir; |
| 287 | } |
| 288 | return 0; |
| 289 | |
| 290 | |
| 291 | out_dir: |
| 292 | if (dir_de) { |
| 293 | kunmap(dir_page); |
| 294 | page_cache_release(dir_page); |
| 295 | } |
| 296 | out_old: |
| 297 | kunmap(old_page); |
| 298 | page_cache_release(old_page); |
| 299 | out: |
| 300 | return err; |
| 301 | } |
| 302 | |
| 303 | const struct inode_operations exofs_dir_inode_operations = { |
| 304 | .create = exofs_create, |
| 305 | .lookup = exofs_lookup, |
| 306 | .link = exofs_link, |
| 307 | .unlink = exofs_unlink, |
| 308 | .symlink = exofs_symlink, |
| 309 | .mkdir = exofs_mkdir, |
| 310 | .rmdir = exofs_rmdir, |
| 311 | .mknod = exofs_mknod, |
| 312 | .rename = exofs_rename, |
| 313 | .setattr = exofs_setattr, |
| 314 | }; |
| 315 | |
| 316 | const struct inode_operations exofs_special_inode_operations = { |
| 317 | .setattr = exofs_setattr, |
| 318 | }; |