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