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