Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/minix/dir.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | * |
| 6 | * minix directory handling functions |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 7 | * |
| 8 | * Updated to filesystem version 3 by Daniel Aragones |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "minix.h" |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 12 | #include <linux/buffer_head.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/highmem.h> |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 14 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | typedef struct minix_dir_entry minix_dirent; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 17 | typedef struct minix3_dir_entry minix3_dirent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | static int minix_readdir(struct file *, void *, filldir_t); |
| 20 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 21 | const struct file_operations minix_dir_operations = { |
Al Viro | cc46759 | 2009-06-16 23:47:45 -0400 | [diff] [blame] | 22 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | .read = generic_read_dir, |
| 24 | .readdir = minix_readdir, |
Al Viro | 0d7916d | 2009-06-07 15:21:06 -0400 | [diff] [blame] | 25 | .fsync = simple_fsync, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | static inline void dir_put_page(struct page *page) |
| 29 | { |
| 30 | kunmap(page); |
| 31 | page_cache_release(page); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Return the offset into page `page_nr' of the last valid |
| 36 | * byte in that page, plus one. |
| 37 | */ |
| 38 | static unsigned |
| 39 | minix_last_byte(struct inode *inode, unsigned long page_nr) |
| 40 | { |
| 41 | unsigned last_byte = PAGE_CACHE_SIZE; |
| 42 | |
| 43 | if (page_nr == (inode->i_size >> PAGE_CACHE_SHIFT)) |
| 44 | last_byte = inode->i_size & (PAGE_CACHE_SIZE - 1); |
| 45 | return last_byte; |
| 46 | } |
| 47 | |
| 48 | static inline unsigned long dir_pages(struct inode *inode) |
| 49 | { |
| 50 | return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT; |
| 51 | } |
| 52 | |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 53 | static int dir_commit_chunk(struct page *page, loff_t pos, unsigned len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 55 | struct address_space *mapping = page->mapping; |
| 56 | struct inode *dir = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | int err = 0; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 58 | block_write_end(NULL, mapping, pos, len, len, page, NULL); |
| 59 | |
| 60 | if (pos+len > dir->i_size) { |
| 61 | i_size_write(dir, pos+len); |
| 62 | mark_inode_dirty(dir); |
| 63 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (IS_DIRSYNC(dir)) |
| 65 | err = write_one_page(page, 1); |
| 66 | else |
| 67 | unlock_page(page); |
| 68 | return err; |
| 69 | } |
| 70 | |
| 71 | static struct page * dir_get_page(struct inode *dir, unsigned long n) |
| 72 | { |
| 73 | struct address_space *mapping = dir->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 74 | struct page *page = read_mapping_page(mapping, n, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | if (!IS_ERR(page)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | kmap(page); |
| 77 | if (!PageUptodate(page)) |
| 78 | goto fail; |
| 79 | } |
| 80 | return page; |
| 81 | |
| 82 | fail: |
| 83 | dir_put_page(page); |
| 84 | return ERR_PTR(-EIO); |
| 85 | } |
| 86 | |
| 87 | static inline void *minix_next_entry(void *de, struct minix_sb_info *sbi) |
| 88 | { |
| 89 | return (void*)((char*)de + sbi->s_dirsize); |
| 90 | } |
| 91 | |
| 92 | static int minix_readdir(struct file * filp, void * dirent, filldir_t filldir) |
| 93 | { |
| 94 | unsigned long pos = filp->f_pos; |
Josef Sipek | dcf258a | 2006-12-08 02:37:19 -0800 | [diff] [blame] | 95 | struct inode *inode = filp->f_path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | struct super_block *sb = inode->i_sb; |
| 97 | unsigned offset = pos & ~PAGE_CACHE_MASK; |
| 98 | unsigned long n = pos >> PAGE_CACHE_SHIFT; |
| 99 | unsigned long npages = dir_pages(inode); |
| 100 | struct minix_sb_info *sbi = minix_sb(sb); |
| 101 | unsigned chunk_size = sbi->s_dirsize; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 102 | char *name; |
| 103 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | pos = (pos + chunk_size-1) & ~(chunk_size-1); |
| 106 | if (pos >= inode->i_size) |
| 107 | goto done; |
| 108 | |
| 109 | for ( ; n < npages; n++, offset = 0) { |
| 110 | char *p, *kaddr, *limit; |
| 111 | struct page *page = dir_get_page(inode, n); |
| 112 | |
| 113 | if (IS_ERR(page)) |
| 114 | continue; |
| 115 | kaddr = (char *)page_address(page); |
| 116 | p = kaddr+offset; |
| 117 | limit = kaddr + minix_last_byte(inode, n) - chunk_size; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 118 | for ( ; p <= limit; p = minix_next_entry(p, sbi)) { |
| 119 | if (sbi->s_version == MINIX_V3) { |
| 120 | minix3_dirent *de3 = (minix3_dirent *)p; |
| 121 | name = de3->name; |
| 122 | inumber = de3->inode; |
| 123 | } else { |
| 124 | minix_dirent *de = (minix_dirent *)p; |
| 125 | name = de->name; |
| 126 | inumber = de->inode; |
| 127 | } |
| 128 | if (inumber) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | int over; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 131 | unsigned l = strnlen(name, sbi->s_namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | offset = p - kaddr; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 133 | over = filldir(dirent, name, l, |
| 134 | (n << PAGE_CACHE_SHIFT) | offset, |
| 135 | inumber, DT_UNKNOWN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | if (over) { |
| 137 | dir_put_page(page); |
| 138 | goto done; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | dir_put_page(page); |
| 143 | } |
| 144 | |
| 145 | done: |
| 146 | filp->f_pos = (n << PAGE_CACHE_SHIFT) | offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static inline int namecompare(int len, int maxlen, |
| 151 | const char * name, const char * buffer) |
| 152 | { |
| 153 | if (len < maxlen && buffer[len]) |
| 154 | return 0; |
| 155 | return !memcmp(name, buffer, len); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * minix_find_entry() |
| 160 | * |
| 161 | * finds an entry in the specified directory with the wanted name. It |
| 162 | * returns the cache buffer in which the entry was found, and the entry |
| 163 | * itself (as a parameter - res_dir). It does NOT read the inode of the |
| 164 | * entry - you'll have to do that yourself if you want to. |
| 165 | */ |
| 166 | minix_dirent *minix_find_entry(struct dentry *dentry, struct page **res_page) |
| 167 | { |
| 168 | const char * name = dentry->d_name.name; |
| 169 | int namelen = dentry->d_name.len; |
| 170 | struct inode * dir = dentry->d_parent->d_inode; |
| 171 | struct super_block * sb = dir->i_sb; |
| 172 | struct minix_sb_info * sbi = minix_sb(sb); |
| 173 | unsigned long n; |
| 174 | unsigned long npages = dir_pages(dir); |
| 175 | struct page *page = NULL; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 176 | char *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 178 | char *namx; |
| 179 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | *res_page = NULL; |
| 181 | |
| 182 | for (n = 0; n < npages; n++) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 183 | char *kaddr, *limit; |
| 184 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | page = dir_get_page(dir, n); |
| 186 | if (IS_ERR(page)) |
| 187 | continue; |
| 188 | |
| 189 | kaddr = (char*)page_address(page); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 190 | limit = kaddr + minix_last_byte(dir, n) - sbi->s_dirsize; |
| 191 | for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) { |
| 192 | if (sbi->s_version == MINIX_V3) { |
| 193 | minix3_dirent *de3 = (minix3_dirent *)p; |
| 194 | namx = de3->name; |
| 195 | inumber = de3->inode; |
| 196 | } else { |
| 197 | minix_dirent *de = (minix_dirent *)p; |
| 198 | namx = de->name; |
| 199 | inumber = de->inode; |
| 200 | } |
| 201 | if (!inumber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | continue; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 203 | if (namecompare(namelen, sbi->s_namelen, name, namx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | goto found; |
| 205 | } |
| 206 | dir_put_page(page); |
| 207 | } |
| 208 | return NULL; |
| 209 | |
| 210 | found: |
| 211 | *res_page = page; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 212 | return (minix_dirent *)p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | int minix_add_link(struct dentry *dentry, struct inode *inode) |
| 216 | { |
| 217 | struct inode *dir = dentry->d_parent->d_inode; |
| 218 | const char * name = dentry->d_name.name; |
| 219 | int namelen = dentry->d_name.len; |
| 220 | struct super_block * sb = dir->i_sb; |
| 221 | struct minix_sb_info * sbi = minix_sb(sb); |
| 222 | struct page *page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | unsigned long npages = dir_pages(dir); |
| 224 | unsigned long n; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 225 | char *kaddr, *p; |
| 226 | minix_dirent *de; |
| 227 | minix3_dirent *de3; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 228 | loff_t pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | int err; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 230 | char *namx = NULL; |
| 231 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | /* |
| 234 | * We take care of directory expansion in the same loop |
| 235 | * This code plays outside i_size, so it locks the page |
| 236 | * to protect that region. |
| 237 | */ |
| 238 | for (n = 0; n <= npages; n++) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 239 | char *limit, *dir_end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | page = dir_get_page(dir, n); |
| 242 | err = PTR_ERR(page); |
| 243 | if (IS_ERR(page)) |
| 244 | goto out; |
| 245 | lock_page(page); |
| 246 | kaddr = (char*)page_address(page); |
| 247 | dir_end = kaddr + minix_last_byte(dir, n); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 248 | limit = kaddr + PAGE_CACHE_SIZE - sbi->s_dirsize; |
| 249 | for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) { |
| 250 | de = (minix_dirent *)p; |
| 251 | de3 = (minix3_dirent *)p; |
| 252 | if (sbi->s_version == MINIX_V3) { |
| 253 | namx = de3->name; |
| 254 | inumber = de3->inode; |
| 255 | } else { |
| 256 | namx = de->name; |
| 257 | inumber = de->inode; |
| 258 | } |
| 259 | if (p == dir_end) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | /* We hit i_size */ |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 261 | if (sbi->s_version == MINIX_V3) |
| 262 | de3->inode = 0; |
| 263 | else |
| 264 | de->inode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | goto got_it; |
| 266 | } |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 267 | if (!inumber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | goto got_it; |
| 269 | err = -EEXIST; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 270 | if (namecompare(namelen, sbi->s_namelen, name, namx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | unlock_page(page); |
| 274 | dir_put_page(page); |
| 275 | } |
| 276 | BUG(); |
| 277 | return -EINVAL; |
| 278 | |
| 279 | got_it: |
Evgeniy Dushistov | d6b5484 | 2009-01-06 14:42:38 -0800 | [diff] [blame] | 280 | pos = page_offset(page) + p - (char *)page_address(page); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 281 | err = __minix_write_begin(NULL, page->mapping, pos, sbi->s_dirsize, |
| 282 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | if (err) |
| 284 | goto out_unlock; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 285 | memcpy (namx, name, namelen); |
| 286 | if (sbi->s_version == MINIX_V3) { |
| 287 | memset (namx + namelen, 0, sbi->s_dirsize - namelen - 4); |
| 288 | de3->inode = inode->i_ino; |
| 289 | } else { |
| 290 | memset (namx + namelen, 0, sbi->s_dirsize - namelen - 2); |
| 291 | de->inode = inode->i_ino; |
| 292 | } |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 293 | err = dir_commit_chunk(page, pos, sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 295 | mark_inode_dirty(dir); |
| 296 | out_put: |
| 297 | dir_put_page(page); |
| 298 | out: |
| 299 | return err; |
| 300 | out_unlock: |
| 301 | unlock_page(page); |
| 302 | goto out_put; |
| 303 | } |
| 304 | |
| 305 | int minix_delete_entry(struct minix_dir_entry *de, struct page *page) |
| 306 | { |
| 307 | struct address_space *mapping = page->mapping; |
| 308 | struct inode *inode = (struct inode*)mapping->host; |
| 309 | char *kaddr = page_address(page); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 310 | loff_t pos = page_offset(page) + (char*)de - kaddr; |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 311 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
| 312 | unsigned len = sbi->s_dirsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | int err; |
| 314 | |
| 315 | lock_page(page); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 316 | err = __minix_write_begin(NULL, mapping, pos, len, |
| 317 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | if (err == 0) { |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 319 | if (sbi->s_version == MINIX_V3) |
| 320 | ((minix3_dirent *) de)->inode = 0; |
| 321 | else |
| 322 | de->inode = 0; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 323 | err = dir_commit_chunk(page, pos, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | } else { |
| 325 | unlock_page(page); |
| 326 | } |
| 327 | dir_put_page(page); |
| 328 | inode->i_ctime = inode->i_mtime = CURRENT_TIME_SEC; |
| 329 | mark_inode_dirty(inode); |
| 330 | return err; |
| 331 | } |
| 332 | |
| 333 | int minix_make_empty(struct inode *inode, struct inode *dir) |
| 334 | { |
| 335 | struct address_space *mapping = inode->i_mapping; |
| 336 | struct page *page = grab_cache_page(mapping, 0); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 337 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | char *kaddr; |
| 339 | int err; |
| 340 | |
| 341 | if (!page) |
| 342 | return -ENOMEM; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 343 | err = __minix_write_begin(NULL, mapping, 0, 2 * sbi->s_dirsize, |
| 344 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if (err) { |
| 346 | unlock_page(page); |
| 347 | goto fail; |
| 348 | } |
| 349 | |
| 350 | kaddr = kmap_atomic(page, KM_USER0); |
| 351 | memset(kaddr, 0, PAGE_CACHE_SIZE); |
| 352 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 353 | if (sbi->s_version == MINIX_V3) { |
| 354 | minix3_dirent *de3 = (minix3_dirent *)kaddr; |
| 355 | |
| 356 | de3->inode = inode->i_ino; |
| 357 | strcpy(de3->name, "."); |
| 358 | de3 = minix_next_entry(de3, sbi); |
| 359 | de3->inode = dir->i_ino; |
| 360 | strcpy(de3->name, ".."); |
| 361 | } else { |
| 362 | minix_dirent *de = (minix_dirent *)kaddr; |
| 363 | |
| 364 | de->inode = inode->i_ino; |
| 365 | strcpy(de->name, "."); |
| 366 | de = minix_next_entry(de, sbi); |
| 367 | de->inode = dir->i_ino; |
| 368 | strcpy(de->name, ".."); |
| 369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | kunmap_atomic(kaddr, KM_USER0); |
| 371 | |
| 372 | err = dir_commit_chunk(page, 0, 2 * sbi->s_dirsize); |
| 373 | fail: |
| 374 | page_cache_release(page); |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | /* |
| 379 | * routine to check that the specified directory is empty (for rmdir) |
| 380 | */ |
| 381 | int minix_empty_dir(struct inode * inode) |
| 382 | { |
| 383 | struct page *page = NULL; |
| 384 | unsigned long i, npages = dir_pages(inode); |
| 385 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 386 | char *name; |
| 387 | __u32 inumber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | for (i = 0; i < npages; i++) { |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 390 | char *p, *kaddr, *limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 392 | page = dir_get_page(inode, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | if (IS_ERR(page)) |
| 394 | continue; |
| 395 | |
| 396 | kaddr = (char *)page_address(page); |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 397 | limit = kaddr + minix_last_byte(inode, i) - sbi->s_dirsize; |
| 398 | for (p = kaddr; p <= limit; p = minix_next_entry(p, sbi)) { |
| 399 | if (sbi->s_version == MINIX_V3) { |
| 400 | minix3_dirent *de3 = (minix3_dirent *)p; |
| 401 | name = de3->name; |
| 402 | inumber = de3->inode; |
| 403 | } else { |
| 404 | minix_dirent *de = (minix_dirent *)p; |
| 405 | name = de->name; |
| 406 | inumber = de->inode; |
| 407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 409 | if (inumber != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* check for . and .. */ |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 411 | if (name[0] != '.') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | goto not_empty; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 413 | if (!name[1]) { |
| 414 | if (inumber != inode->i_ino) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | goto not_empty; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 416 | } else if (name[1] != '.') |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | goto not_empty; |
Andries Brouwer | 939b00d | 2007-02-12 00:52:49 -0800 | [diff] [blame] | 418 | else if (name[2]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | goto not_empty; |
| 420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | dir_put_page(page); |
| 423 | } |
| 424 | return 1; |
| 425 | |
| 426 | not_empty: |
| 427 | dir_put_page(page); |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* Releases the page */ |
| 432 | void minix_set_link(struct minix_dir_entry *de, struct page *page, |
| 433 | struct inode *inode) |
| 434 | { |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 435 | struct address_space *mapping = page->mapping; |
| 436 | struct inode *dir = mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | struct minix_sb_info *sbi = minix_sb(dir->i_sb); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 438 | loff_t pos = page_offset(page) + |
| 439 | (char *)de-(char*)page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | int err; |
| 441 | |
| 442 | lock_page(page); |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 443 | |
| 444 | err = __minix_write_begin(NULL, mapping, pos, sbi->s_dirsize, |
| 445 | AOP_FLAG_UNINTERRUPTIBLE, &page, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | if (err == 0) { |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 447 | if (sbi->s_version == MINIX_V3) |
| 448 | ((minix3_dirent *) de)->inode = inode->i_ino; |
| 449 | else |
| 450 | de->inode = inode->i_ino; |
Nick Piggin | 4a66af9 | 2007-10-16 01:25:21 -0700 | [diff] [blame] | 451 | err = dir_commit_chunk(page, pos, sbi->s_dirsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } else { |
| 453 | unlock_page(page); |
| 454 | } |
| 455 | dir_put_page(page); |
| 456 | dir->i_mtime = dir->i_ctime = CURRENT_TIME_SEC; |
| 457 | mark_inode_dirty(dir); |
| 458 | } |
| 459 | |
| 460 | struct minix_dir_entry * minix_dotdot (struct inode *dir, struct page **p) |
| 461 | { |
| 462 | struct page *page = dir_get_page(dir, 0); |
| 463 | struct minix_sb_info *sbi = minix_sb(dir->i_sb); |
| 464 | struct minix_dir_entry *de = NULL; |
| 465 | |
| 466 | if (!IS_ERR(page)) { |
| 467 | de = minix_next_entry(page_address(page), sbi); |
| 468 | *p = page; |
| 469 | } |
| 470 | return de; |
| 471 | } |
| 472 | |
| 473 | ino_t minix_inode_by_name(struct dentry *dentry) |
| 474 | { |
| 475 | struct page *page; |
| 476 | struct minix_dir_entry *de = minix_find_entry(dentry, &page); |
| 477 | ino_t res = 0; |
| 478 | |
| 479 | if (de) { |
Doug Graham | 9f6c133 | 2009-09-22 16:48:00 -0700 | [diff] [blame] | 480 | struct address_space *mapping = page->mapping; |
| 481 | struct inode *inode = mapping->host; |
| 482 | struct minix_sb_info *sbi = minix_sb(inode->i_sb); |
| 483 | |
| 484 | if (sbi->s_version == MINIX_V3) |
| 485 | res = ((minix3_dirent *) de)->inode; |
| 486 | else |
| 487 | res = de->inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | dir_put_page(page); |
| 489 | } |
| 490 | return res; |
| 491 | } |