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/hpfs/ea.c |
| 4 | * |
| 5 | * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 |
| 6 | * |
| 7 | * handling extended attributes |
| 8 | */ |
| 9 | |
| 10 | #include "hpfs_fn.h" |
| 11 | |
| 12 | /* Remove external extended attributes. ano specifies whether a is a |
| 13 | direct sector where eas starts or an anode */ |
| 14 | |
| 15 | void hpfs_ea_ext_remove(struct super_block *s, secno a, int ano, unsigned len) |
| 16 | { |
| 17 | unsigned pos = 0; |
| 18 | while (pos < len) { |
| 19 | char ex[4 + 255 + 1 + 8]; |
| 20 | struct extended_attribute *ea = (struct extended_attribute *)ex; |
| 21 | if (pos + 4 > len) { |
| 22 | hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x", |
| 23 | ano ? "anode" : "sectors", a, len); |
| 24 | return; |
| 25 | } |
| 26 | if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return; |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 27 | if (ea_indirect(ea)) { |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 28 | if (ea_valuelen(ea) != 8) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 29 | hpfs_error(s, "ea_indirect(ea) set while ea->valuelen!=8, %s %08x, pos %08x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | ano ? "anode" : "sectors", a, pos); |
| 31 | return; |
| 32 | } |
| 33 | if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 9, ex+4)) |
| 34 | return; |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 35 | hpfs_ea_remove(s, ea_sec(ea), ea_in_anode(ea), ea_len(ea)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 37 | pos += ea->namelen + ea_valuelen(ea) + 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | if (!ano) hpfs_free_sectors(s, a, (len+511) >> 9); |
| 40 | else { |
| 41 | struct buffer_head *bh; |
| 42 | struct anode *anode; |
| 43 | if ((anode = hpfs_map_anode(s, a, &bh))) { |
| 44 | hpfs_remove_btree(s, &anode->btree); |
| 45 | brelse(bh); |
| 46 | hpfs_free_sectors(s, a, 1); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | static char *get_indirect_ea(struct super_block *s, int ano, secno a, int size) |
| 52 | { |
| 53 | char *ret; |
| 54 | if (!(ret = kmalloc(size + 1, GFP_NOFS))) { |
Fabian Frederick | a19189e | 2014-06-06 14:36:36 -0700 | [diff] [blame] | 55 | pr_err("out of memory for EA\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return NULL; |
| 57 | } |
| 58 | if (hpfs_ea_read(s, a, ano, 0, size, ret)) { |
| 59 | kfree(ret); |
| 60 | return NULL; |
| 61 | } |
| 62 | ret[size] = 0; |
| 63 | return ret; |
| 64 | } |
| 65 | |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 66 | static void set_indirect_ea(struct super_block *s, int ano, secno a, |
| 67 | const char *data, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | hpfs_ea_write(s, a, ano, 0, size, data); |
| 70 | } |
| 71 | |
| 72 | /* Read an extended attribute named 'key' into the provided buffer */ |
| 73 | |
| 74 | int hpfs_read_ea(struct super_block *s, struct fnode *fnode, char *key, |
| 75 | char *buf, int size) |
| 76 | { |
| 77 | unsigned pos; |
| 78 | int ano, len; |
| 79 | secno a; |
Mikulas Patocka | c351481 | 2011-05-08 20:44:38 +0200 | [diff] [blame] | 80 | char ex[4 + 255 + 1 + 8]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | struct extended_attribute *ea; |
| 82 | struct extended_attribute *ea_end = fnode_end_ea(fnode); |
| 83 | for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea)) |
| 84 | if (!strcmp(ea->name, key)) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 85 | if (ea_indirect(ea)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | goto indirect; |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 87 | if (ea_valuelen(ea) >= size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | return -EINVAL; |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 89 | memcpy(buf, ea_data(ea), ea_valuelen(ea)); |
| 90 | buf[ea_valuelen(ea)] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 93 | a = le32_to_cpu(fnode->ea_secno); |
| 94 | len = le32_to_cpu(fnode->ea_size_l); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 95 | ano = fnode_in_anode(fnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | pos = 0; |
| 97 | while (pos < len) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | ea = (struct extended_attribute *)ex; |
| 99 | if (pos + 4 > len) { |
| 100 | hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x", |
| 101 | ano ? "anode" : "sectors", a, len); |
| 102 | return -EIO; |
| 103 | } |
| 104 | if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return -EIO; |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 105 | if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 1 + (ea_indirect(ea) ? 8 : 0), ex + 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return -EIO; |
| 107 | if (!strcmp(ea->name, key)) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 108 | if (ea_indirect(ea)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | goto indirect; |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 110 | if (ea_valuelen(ea) >= size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return -EINVAL; |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 112 | if (hpfs_ea_read(s, a, ano, pos + 4 + ea->namelen + 1, ea_valuelen(ea), buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return -EIO; |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 114 | buf[ea_valuelen(ea)] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | return 0; |
| 116 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 117 | pos += ea->namelen + ea_valuelen(ea) + 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | } |
| 119 | return -ENOENT; |
| 120 | indirect: |
| 121 | if (ea_len(ea) >= size) |
| 122 | return -EINVAL; |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 123 | if (hpfs_ea_read(s, ea_sec(ea), ea_in_anode(ea), 0, ea_len(ea), buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return -EIO; |
| 125 | buf[ea_len(ea)] = 0; |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /* Read an extended attribute named 'key' */ |
| 130 | char *hpfs_get_ea(struct super_block *s, struct fnode *fnode, char *key, int *size) |
| 131 | { |
| 132 | char *ret; |
| 133 | unsigned pos; |
| 134 | int ano, len; |
| 135 | secno a; |
| 136 | struct extended_attribute *ea; |
| 137 | struct extended_attribute *ea_end = fnode_end_ea(fnode); |
| 138 | for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea)) |
| 139 | if (!strcmp(ea->name, key)) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 140 | if (ea_indirect(ea)) |
| 141 | return get_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), *size = ea_len(ea)); |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 142 | if (!(ret = kmalloc((*size = ea_valuelen(ea)) + 1, GFP_NOFS))) { |
Fabian Frederick | a19189e | 2014-06-06 14:36:36 -0700 | [diff] [blame] | 143 | pr_err("out of memory for EA\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return NULL; |
| 145 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 146 | memcpy(ret, ea_data(ea), ea_valuelen(ea)); |
| 147 | ret[ea_valuelen(ea)] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | return ret; |
| 149 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 150 | a = le32_to_cpu(fnode->ea_secno); |
| 151 | len = le32_to_cpu(fnode->ea_size_l); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 152 | ano = fnode_in_anode(fnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | pos = 0; |
| 154 | while (pos < len) { |
| 155 | char ex[4 + 255 + 1 + 8]; |
| 156 | ea = (struct extended_attribute *)ex; |
| 157 | if (pos + 4 > len) { |
| 158 | hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x", |
| 159 | ano ? "anode" : "sectors", a, len); |
| 160 | return NULL; |
| 161 | } |
| 162 | if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return NULL; |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 163 | if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 1 + (ea_indirect(ea) ? 8 : 0), ex + 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | return NULL; |
| 165 | if (!strcmp(ea->name, key)) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 166 | if (ea_indirect(ea)) |
| 167 | return get_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), *size = ea_len(ea)); |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 168 | if (!(ret = kmalloc((*size = ea_valuelen(ea)) + 1, GFP_NOFS))) { |
Fabian Frederick | a19189e | 2014-06-06 14:36:36 -0700 | [diff] [blame] | 169 | pr_err("out of memory for EA\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return NULL; |
| 171 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 172 | if (hpfs_ea_read(s, a, ano, pos + 4 + ea->namelen + 1, ea_valuelen(ea), ret)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | kfree(ret); |
| 174 | return NULL; |
| 175 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 176 | ret[ea_valuelen(ea)] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return ret; |
| 178 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 179 | pos += ea->namelen + ea_valuelen(ea) + 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Update or create extended attribute 'key' with value 'data'. Note that |
| 186 | * when this ea exists, it MUST have the same size as size of data. |
| 187 | * This driver can't change sizes of eas ('cause I just don't need it). |
| 188 | */ |
| 189 | |
Al Viro | 7e7742e | 2010-01-31 17:09:29 -0500 | [diff] [blame] | 190 | void hpfs_set_ea(struct inode *inode, struct fnode *fnode, const char *key, |
| 191 | const char *data, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | fnode_secno fno = inode->i_ino; |
| 194 | struct super_block *s = inode->i_sb; |
| 195 | unsigned pos; |
| 196 | int ano, len; |
| 197 | secno a; |
| 198 | unsigned char h[4]; |
| 199 | struct extended_attribute *ea; |
| 200 | struct extended_attribute *ea_end = fnode_end_ea(fnode); |
| 201 | for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea)) |
| 202 | if (!strcmp(ea->name, key)) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 203 | if (ea_indirect(ea)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (ea_len(ea) == size) |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 205 | set_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), data, size); |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 206 | } else if (ea_valuelen(ea) == size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | memcpy(ea_data(ea), data, size); |
| 208 | } |
| 209 | return; |
| 210 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 211 | a = le32_to_cpu(fnode->ea_secno); |
| 212 | len = le32_to_cpu(fnode->ea_size_l); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 213 | ano = fnode_in_anode(fnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | pos = 0; |
| 215 | while (pos < len) { |
| 216 | char ex[4 + 255 + 1 + 8]; |
| 217 | ea = (struct extended_attribute *)ex; |
| 218 | if (pos + 4 > len) { |
| 219 | hpfs_error(s, "EAs don't end correctly, %s %08x, len %08x", |
| 220 | ano ? "anode" : "sectors", a, len); |
| 221 | return; |
| 222 | } |
| 223 | if (hpfs_ea_read(s, a, ano, pos, 4, ex)) return; |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 224 | if (hpfs_ea_read(s, a, ano, pos + 4, ea->namelen + 1 + (ea_indirect(ea) ? 8 : 0), ex + 4)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | return; |
| 226 | if (!strcmp(ea->name, key)) { |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 227 | if (ea_indirect(ea)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | if (ea_len(ea) == size) |
Al Viro | 4085e15 | 2012-04-06 13:21:09 -0400 | [diff] [blame] | 229 | set_indirect_ea(s, ea_in_anode(ea), ea_sec(ea), data, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | else { |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 232 | if (ea_valuelen(ea) == size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | hpfs_ea_write(s, a, ano, pos + 4 + ea->namelen + 1, size, data); |
| 234 | } |
| 235 | return; |
| 236 | } |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 237 | pos += ea->namelen + ea_valuelen(ea) + 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 239 | if (!le16_to_cpu(fnode->ea_offs)) { |
| 240 | /*if (le16_to_cpu(fnode->ea_size_s)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | hpfs_error(s, "fnode %08x: ea_size_s == %03x, ea_offs == 0", |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 242 | inode->i_ino, le16_to_cpu(fnode->ea_size_s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return; |
| 244 | }*/ |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 245 | fnode->ea_offs = cpu_to_le16(0xc4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 247 | if (le16_to_cpu(fnode->ea_offs) < 0xc4 || le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s) + le16_to_cpu(fnode->ea_size_s) > 0x200) { |
Randy Dunlap | 18debbb | 2006-12-06 20:37:05 -0800 | [diff] [blame] | 248 | hpfs_error(s, "fnode %08lx: ea_offs == %03x, ea_size_s == %03x", |
| 249 | (unsigned long)inode->i_ino, |
Al Viro | de5e2b3 | 2012-04-13 01:24:37 -0400 | [diff] [blame] | 250 | le16_to_cpu(fnode->ea_offs), le16_to_cpu(fnode->ea_size_s)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return; |
| 252 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 253 | if ((le16_to_cpu(fnode->ea_size_s) || !le32_to_cpu(fnode->ea_size_l)) && |
| 254 | le16_to_cpu(fnode->ea_offs) + le16_to_cpu(fnode->acl_size_s) + le16_to_cpu(fnode->ea_size_s) + strlen(key) + size + 5 <= 0x200) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | ea = fnode_end_ea(fnode); |
| 256 | *(char *)ea = 0; |
| 257 | ea->namelen = strlen(key); |
Mikulas Patocka | d0969d1 | 2011-05-08 20:44:32 +0200 | [diff] [blame] | 258 | ea->valuelen_lo = size; |
| 259 | ea->valuelen_hi = size >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | strcpy(ea->name, key); |
| 261 | memcpy(ea_data(ea), data, size); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 262 | fnode->ea_size_s = cpu_to_le16(le16_to_cpu(fnode->ea_size_s) + strlen(key) + size + 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | goto ret; |
| 264 | } |
| 265 | /* Most the code here is 99.9993422% unused. I hope there are no bugs. |
| 266 | But what .. HPFS.IFS has also bugs in ea management. */ |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 267 | if (le16_to_cpu(fnode->ea_size_s) && !le32_to_cpu(fnode->ea_size_l)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | secno n; |
| 269 | struct buffer_head *bh; |
| 270 | char *data; |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 271 | if (!(n = hpfs_alloc_sector(s, fno, 1, 0))) return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | if (!(data = hpfs_get_sector(s, n, &bh))) { |
| 273 | hpfs_free_sectors(s, n, 1); |
| 274 | return; |
| 275 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 276 | memcpy(data, fnode_ea(fnode), le16_to_cpu(fnode->ea_size_s)); |
| 277 | fnode->ea_size_l = cpu_to_le32(le16_to_cpu(fnode->ea_size_s)); |
| 278 | fnode->ea_size_s = cpu_to_le16(0); |
| 279 | fnode->ea_secno = cpu_to_le32(n); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 280 | fnode->flags &= ~FNODE_anode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | mark_buffer_dirty(bh); |
| 282 | brelse(bh); |
| 283 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 284 | pos = le32_to_cpu(fnode->ea_size_l) + 5 + strlen(key) + size; |
| 285 | len = (le32_to_cpu(fnode->ea_size_l) + 511) >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | if (pos >= 30000) goto bail; |
| 287 | while (((pos + 511) >> 9) > len) { |
| 288 | if (!len) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 289 | secno q = hpfs_alloc_sector(s, fno, 1, 0); |
| 290 | if (!q) goto bail; |
| 291 | fnode->ea_secno = cpu_to_le32(q); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 292 | fnode->flags &= ~FNODE_anode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | len++; |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 294 | } else if (!fnode_in_anode(fnode)) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 295 | if (hpfs_alloc_if_possible(s, le32_to_cpu(fnode->ea_secno) + len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | len++; |
| 297 | } else { |
| 298 | /* Aargh... don't know how to create ea anodes :-( */ |
| 299 | /*struct buffer_head *bh; |
| 300 | struct anode *anode; |
| 301 | anode_secno a_s; |
| 302 | if (!(anode = hpfs_alloc_anode(s, fno, &a_s, &bh))) |
| 303 | goto bail; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 304 | anode->up = cpu_to_le32(fno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | anode->btree.fnode_parent = 1; |
| 306 | anode->btree.n_free_nodes--; |
| 307 | anode->btree.n_used_nodes++; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 308 | anode->btree.first_free = cpu_to_le16(le16_to_cpu(anode->btree.first_free) + 12); |
| 309 | anode->u.external[0].disk_secno = cpu_to_le32(le32_to_cpu(fnode->ea_secno)); |
| 310 | anode->u.external[0].file_secno = cpu_to_le32(0); |
| 311 | anode->u.external[0].length = cpu_to_le32(len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | mark_buffer_dirty(bh); |
| 313 | brelse(bh); |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 314 | fnode->flags |= FNODE_anode; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 315 | fnode->ea_secno = cpu_to_le32(a_s);*/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | secno new_sec; |
| 317 | int i; |
Mikulas Patocka | 7d23ce3 | 2011-05-08 20:43:06 +0200 | [diff] [blame] | 318 | if (!(new_sec = hpfs_alloc_sector(s, fno, 1, 1 - ((pos + 511) >> 9)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | goto bail; |
| 320 | for (i = 0; i < len; i++) { |
| 321 | struct buffer_head *bh1, *bh2; |
| 322 | void *b1, *b2; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 323 | if (!(b1 = hpfs_map_sector(s, le32_to_cpu(fnode->ea_secno) + i, &bh1, len - i - 1))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | hpfs_free_sectors(s, new_sec, (pos + 511) >> 9); |
| 325 | goto bail; |
| 326 | } |
| 327 | if (!(b2 = hpfs_get_sector(s, new_sec + i, &bh2))) { |
| 328 | brelse(bh1); |
| 329 | hpfs_free_sectors(s, new_sec, (pos + 511) >> 9); |
| 330 | goto bail; |
| 331 | } |
| 332 | memcpy(b2, b1, 512); |
| 333 | brelse(bh1); |
| 334 | mark_buffer_dirty(bh2); |
| 335 | brelse(bh2); |
| 336 | } |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 337 | hpfs_free_sectors(s, le32_to_cpu(fnode->ea_secno), len); |
| 338 | fnode->ea_secno = cpu_to_le32(new_sec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | len = (pos + 511) >> 9; |
| 340 | } |
| 341 | } |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 342 | if (fnode_in_anode(fnode)) { |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 343 | if (hpfs_add_sector_to_btree(s, le32_to_cpu(fnode->ea_secno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | 0, len) != -1) { |
| 345 | len++; |
| 346 | } else { |
| 347 | goto bail; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | h[0] = 0; |
| 352 | h[1] = strlen(key); |
| 353 | h[2] = size & 0xff; |
| 354 | h[3] = size >> 8; |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 355 | if (hpfs_ea_write(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l), 4, h)) goto bail; |
| 356 | if (hpfs_ea_write(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l) + 4, h[1] + 1, key)) goto bail; |
| 357 | if (hpfs_ea_write(s, le32_to_cpu(fnode->ea_secno), fnode_in_anode(fnode), le32_to_cpu(fnode->ea_size_l) + 5 + h[1], size, data)) goto bail; |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 358 | fnode->ea_size_l = cpu_to_le32(pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | ret: |
| 360 | hpfs_i(inode)->i_ea_size += 5 + strlen(key) + size; |
| 361 | return; |
| 362 | bail: |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 363 | if (le32_to_cpu(fnode->ea_secno)) |
Al Viro | c4c9954 | 2012-04-06 14:30:07 -0400 | [diff] [blame] | 364 | if (fnode_in_anode(fnode)) hpfs_truncate_btree(s, le32_to_cpu(fnode->ea_secno), 1, (le32_to_cpu(fnode->ea_size_l) + 511) >> 9); |
Mikulas Patocka | 0b69760 | 2011-05-08 20:44:26 +0200 | [diff] [blame] | 365 | else hpfs_free_sectors(s, le32_to_cpu(fnode->ea_secno) + ((le32_to_cpu(fnode->ea_size_l) + 511) >> 9), len - ((le32_to_cpu(fnode->ea_size_l) + 511) >> 9)); |
| 366 | else fnode->ea_secno = fnode->ea_size_l = cpu_to_le32(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |