hfs: add error checking for hfs_find_init()
[firefly-linux-kernel-4.4.55.git] / fs / hfs / extent.c
1 /*
2  *  linux/fs/hfs/extent.c
3  *
4  * Copyright (C) 1995-1997  Paul H. Hargrove
5  * (C) 2003 Ardis Technologies <roman@ardistech.com>
6  * This file may be distributed under the terms of the GNU General Public License.
7  *
8  * This file contains the functions related to the extents B-tree.
9  */
10
11 #include <linux/pagemap.h>
12
13 #include "hfs_fs.h"
14 #include "btree.h"
15
16 /*================ File-local functions ================*/
17
18 /*
19  * build_key
20  */
21 static void hfs_ext_build_key(hfs_btree_key *key, u32 cnid, u16 block, u8 type)
22 {
23         key->key_len = 7;
24         key->ext.FkType = type;
25         key->ext.FNum = cpu_to_be32(cnid);
26         key->ext.FABN = cpu_to_be16(block);
27 }
28
29 /*
30  * hfs_ext_compare()
31  *
32  * Description:
33  *   This is the comparison function used for the extents B-tree.  In
34  *   comparing extent B-tree entries, the file id is the most
35  *   significant field (compared as unsigned ints); the fork type is
36  *   the second most significant field (compared as unsigned chars);
37  *   and the allocation block number field is the least significant
38  *   (compared as unsigned ints).
39  * Input Variable(s):
40  *   struct hfs_ext_key *key1: pointer to the first key to compare
41  *   struct hfs_ext_key *key2: pointer to the second key to compare
42  * Output Variable(s):
43  *   NONE
44  * Returns:
45  *   int: negative if key1<key2, positive if key1>key2, and 0 if key1==key2
46  * Preconditions:
47  *   key1 and key2 point to "valid" (struct hfs_ext_key)s.
48  * Postconditions:
49  *   This function has no side-effects */
50 int hfs_ext_keycmp(const btree_key *key1, const btree_key *key2)
51 {
52         __be32 fnum1, fnum2;
53         __be16 block1, block2;
54
55         fnum1 = key1->ext.FNum;
56         fnum2 = key2->ext.FNum;
57         if (fnum1 != fnum2)
58                 return be32_to_cpu(fnum1) < be32_to_cpu(fnum2) ? -1 : 1;
59         if (key1->ext.FkType != key2->ext.FkType)
60                 return key1->ext.FkType < key2->ext.FkType ? -1 : 1;
61
62         block1 = key1->ext.FABN;
63         block2 = key2->ext.FABN;
64         if (block1 == block2)
65                 return 0;
66         return be16_to_cpu(block1) < be16_to_cpu(block2) ? -1 : 1;
67 }
68
69 /*
70  * hfs_ext_find_block
71  *
72  * Find a block within an extent record
73  */
74 static u16 hfs_ext_find_block(struct hfs_extent *ext, u16 off)
75 {
76         int i;
77         u16 count;
78
79         for (i = 0; i < 3; ext++, i++) {
80                 count = be16_to_cpu(ext->count);
81                 if (off < count)
82                         return be16_to_cpu(ext->block) + off;
83                 off -= count;
84         }
85         /* panic? */
86         return 0;
87 }
88
89 static int hfs_ext_block_count(struct hfs_extent *ext)
90 {
91         int i;
92         u16 count = 0;
93
94         for (i = 0; i < 3; ext++, i++)
95                 count += be16_to_cpu(ext->count);
96         return count;
97 }
98
99 static u16 hfs_ext_lastblock(struct hfs_extent *ext)
100 {
101         int i;
102
103         ext += 2;
104         for (i = 0; i < 2; ext--, i++)
105                 if (ext->count)
106                         break;
107         return be16_to_cpu(ext->block) + be16_to_cpu(ext->count);
108 }
109
110 static int __hfs_ext_write_extent(struct inode *inode, struct hfs_find_data *fd)
111 {
112         int res;
113
114         hfs_ext_build_key(fd->search_key, inode->i_ino, HFS_I(inode)->cached_start,
115                           HFS_IS_RSRC(inode) ?  HFS_FK_RSRC : HFS_FK_DATA);
116         res = hfs_brec_find(fd);
117         if (HFS_I(inode)->flags & HFS_FLG_EXT_NEW) {
118                 if (res != -ENOENT)
119                         return res;
120                 hfs_brec_insert(fd, HFS_I(inode)->cached_extents, sizeof(hfs_extent_rec));
121                 HFS_I(inode)->flags &= ~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
122         } else {
123                 if (res)
124                         return res;
125                 hfs_bnode_write(fd->bnode, HFS_I(inode)->cached_extents, fd->entryoffset, fd->entrylength);
126                 HFS_I(inode)->flags &= ~HFS_FLG_EXT_DIRTY;
127         }
128         return 0;
129 }
130
131 int hfs_ext_write_extent(struct inode *inode)
132 {
133         struct hfs_find_data fd;
134         int res = 0;
135
136         if (HFS_I(inode)->flags & HFS_FLG_EXT_DIRTY) {
137                 res = hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, &fd);
138                 if (res)
139                         return res;
140                 res = __hfs_ext_write_extent(inode, &fd);
141                 hfs_find_exit(&fd);
142         }
143         return res;
144 }
145
146 static inline int __hfs_ext_read_extent(struct hfs_find_data *fd, struct hfs_extent *extent,
147                                         u32 cnid, u32 block, u8 type)
148 {
149         int res;
150
151         hfs_ext_build_key(fd->search_key, cnid, block, type);
152         fd->key->ext.FNum = 0;
153         res = hfs_brec_find(fd);
154         if (res && res != -ENOENT)
155                 return res;
156         if (fd->key->ext.FNum != fd->search_key->ext.FNum ||
157             fd->key->ext.FkType != fd->search_key->ext.FkType)
158                 return -ENOENT;
159         if (fd->entrylength != sizeof(hfs_extent_rec))
160                 return -EIO;
161         hfs_bnode_read(fd->bnode, extent, fd->entryoffset, sizeof(hfs_extent_rec));
162         return 0;
163 }
164
165 static inline int __hfs_ext_cache_extent(struct hfs_find_data *fd, struct inode *inode, u32 block)
166 {
167         int res;
168
169         if (HFS_I(inode)->flags & HFS_FLG_EXT_DIRTY) {
170                 res = __hfs_ext_write_extent(inode, fd);
171                 if (res)
172                         return res;
173         }
174
175         res = __hfs_ext_read_extent(fd, HFS_I(inode)->cached_extents, inode->i_ino,
176                                     block, HFS_IS_RSRC(inode) ? HFS_FK_RSRC : HFS_FK_DATA);
177         if (!res) {
178                 HFS_I(inode)->cached_start = be16_to_cpu(fd->key->ext.FABN);
179                 HFS_I(inode)->cached_blocks = hfs_ext_block_count(HFS_I(inode)->cached_extents);
180         } else {
181                 HFS_I(inode)->cached_start = HFS_I(inode)->cached_blocks = 0;
182                 HFS_I(inode)->flags &= ~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
183         }
184         return res;
185 }
186
187 static int hfs_ext_read_extent(struct inode *inode, u16 block)
188 {
189         struct hfs_find_data fd;
190         int res;
191
192         if (block >= HFS_I(inode)->cached_start &&
193             block < HFS_I(inode)->cached_start + HFS_I(inode)->cached_blocks)
194                 return 0;
195
196         res = hfs_find_init(HFS_SB(inode->i_sb)->ext_tree, &fd);
197         if (!res) {
198                 res = __hfs_ext_cache_extent(&fd, inode, block);
199                 hfs_find_exit(&fd);
200         }
201         return res;
202 }
203
204 static void hfs_dump_extent(struct hfs_extent *extent)
205 {
206         int i;
207
208         dprint(DBG_EXTENT, "   ");
209         for (i = 0; i < 3; i++)
210                 dprint(DBG_EXTENT, " %u:%u", be16_to_cpu(extent[i].block),
211                                  be16_to_cpu(extent[i].count));
212         dprint(DBG_EXTENT, "\n");
213 }
214
215 static int hfs_add_extent(struct hfs_extent *extent, u16 offset,
216                           u16 alloc_block, u16 block_count)
217 {
218         u16 count, start;
219         int i;
220
221         hfs_dump_extent(extent);
222         for (i = 0; i < 3; extent++, i++) {
223                 count = be16_to_cpu(extent->count);
224                 if (offset == count) {
225                         start = be16_to_cpu(extent->block);
226                         if (alloc_block != start + count) {
227                                 if (++i >= 3)
228                                         return -ENOSPC;
229                                 extent++;
230                                 extent->block = cpu_to_be16(alloc_block);
231                         } else
232                                 block_count += count;
233                         extent->count = cpu_to_be16(block_count);
234                         return 0;
235                 } else if (offset < count)
236                         break;
237                 offset -= count;
238         }
239         /* panic? */
240         return -EIO;
241 }
242
243 static int hfs_free_extents(struct super_block *sb, struct hfs_extent *extent,
244                             u16 offset, u16 block_nr)
245 {
246         u16 count, start;
247         int i;
248
249         hfs_dump_extent(extent);
250         for (i = 0; i < 3; extent++, i++) {
251                 count = be16_to_cpu(extent->count);
252                 if (offset == count)
253                         goto found;
254                 else if (offset < count)
255                         break;
256                 offset -= count;
257         }
258         /* panic? */
259         return -EIO;
260 found:
261         for (;;) {
262                 start = be16_to_cpu(extent->block);
263                 if (count <= block_nr) {
264                         hfs_clear_vbm_bits(sb, start, count);
265                         extent->block = 0;
266                         extent->count = 0;
267                         block_nr -= count;
268                 } else {
269                         count -= block_nr;
270                         hfs_clear_vbm_bits(sb, start + count, block_nr);
271                         extent->count = cpu_to_be16(count);
272                         block_nr = 0;
273                 }
274                 if (!block_nr || !i)
275                         return 0;
276                 i--;
277                 extent--;
278                 count = be16_to_cpu(extent->count);
279         }
280 }
281
282 int hfs_free_fork(struct super_block *sb, struct hfs_cat_file *file, int type)
283 {
284         struct hfs_find_data fd;
285         u32 total_blocks, blocks, start;
286         u32 cnid = be32_to_cpu(file->FlNum);
287         struct hfs_extent *extent;
288         int res, i;
289
290         if (type == HFS_FK_DATA) {
291                 total_blocks = be32_to_cpu(file->PyLen);
292                 extent = file->ExtRec;
293         } else {
294                 total_blocks = be32_to_cpu(file->RPyLen);
295                 extent = file->RExtRec;
296         }
297         total_blocks /= HFS_SB(sb)->alloc_blksz;
298         if (!total_blocks)
299                 return 0;
300
301         blocks = 0;
302         for (i = 0; i < 3; extent++, i++)
303                 blocks += be16_to_cpu(extent[i].count);
304
305         res = hfs_free_extents(sb, extent, blocks, blocks);
306         if (res)
307                 return res;
308         if (total_blocks == blocks)
309                 return 0;
310
311         res = hfs_find_init(HFS_SB(sb)->ext_tree, &fd);
312         if (res)
313                 return res;
314         do {
315                 res = __hfs_ext_read_extent(&fd, extent, cnid, total_blocks, type);
316                 if (res)
317                         break;
318                 start = be16_to_cpu(fd.key->ext.FABN);
319                 hfs_free_extents(sb, extent, total_blocks - start, total_blocks);
320                 hfs_brec_remove(&fd);
321                 total_blocks = start;
322         } while (total_blocks > blocks);
323         hfs_find_exit(&fd);
324
325         return res;
326 }
327
328 /*
329  * hfs_get_block
330  */
331 int hfs_get_block(struct inode *inode, sector_t block,
332                   struct buffer_head *bh_result, int create)
333 {
334         struct super_block *sb;
335         u16 dblock, ablock;
336         int res;
337
338         sb = inode->i_sb;
339         /* Convert inode block to disk allocation block */
340         ablock = (u32)block / HFS_SB(sb)->fs_div;
341
342         if (block >= HFS_I(inode)->fs_blocks) {
343                 if (block > HFS_I(inode)->fs_blocks || !create)
344                         return -EIO;
345                 if (ablock >= HFS_I(inode)->alloc_blocks) {
346                         res = hfs_extend_file(inode);
347                         if (res)
348                                 return res;
349                 }
350         } else
351                 create = 0;
352
353         if (ablock < HFS_I(inode)->first_blocks) {
354                 dblock = hfs_ext_find_block(HFS_I(inode)->first_extents, ablock);
355                 goto done;
356         }
357
358         mutex_lock(&HFS_I(inode)->extents_lock);
359         res = hfs_ext_read_extent(inode, ablock);
360         if (!res)
361                 dblock = hfs_ext_find_block(HFS_I(inode)->cached_extents,
362                                             ablock - HFS_I(inode)->cached_start);
363         else {
364                 mutex_unlock(&HFS_I(inode)->extents_lock);
365                 return -EIO;
366         }
367         mutex_unlock(&HFS_I(inode)->extents_lock);
368
369 done:
370         map_bh(bh_result, sb, HFS_SB(sb)->fs_start +
371                dblock * HFS_SB(sb)->fs_div +
372                (u32)block % HFS_SB(sb)->fs_div);
373
374         if (create) {
375                 set_buffer_new(bh_result);
376                 HFS_I(inode)->phys_size += sb->s_blocksize;
377                 HFS_I(inode)->fs_blocks++;
378                 inode_add_bytes(inode, sb->s_blocksize);
379                 mark_inode_dirty(inode);
380         }
381         return 0;
382 }
383
384 int hfs_extend_file(struct inode *inode)
385 {
386         struct super_block *sb = inode->i_sb;
387         u32 start, len, goal;
388         int res;
389
390         mutex_lock(&HFS_I(inode)->extents_lock);
391         if (HFS_I(inode)->alloc_blocks == HFS_I(inode)->first_blocks)
392                 goal = hfs_ext_lastblock(HFS_I(inode)->first_extents);
393         else {
394                 res = hfs_ext_read_extent(inode, HFS_I(inode)->alloc_blocks);
395                 if (res)
396                         goto out;
397                 goal = hfs_ext_lastblock(HFS_I(inode)->cached_extents);
398         }
399
400         len = HFS_I(inode)->clump_blocks;
401         start = hfs_vbm_search_free(sb, goal, &len);
402         if (!len) {
403                 res = -ENOSPC;
404                 goto out;
405         }
406
407         dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
408         if (HFS_I(inode)->alloc_blocks == HFS_I(inode)->first_blocks) {
409                 if (!HFS_I(inode)->first_blocks) {
410                         dprint(DBG_EXTENT, "first extents\n");
411                         /* no extents yet */
412                         HFS_I(inode)->first_extents[0].block = cpu_to_be16(start);
413                         HFS_I(inode)->first_extents[0].count = cpu_to_be16(len);
414                         res = 0;
415                 } else {
416                         /* try to append to extents in inode */
417                         res = hfs_add_extent(HFS_I(inode)->first_extents,
418                                              HFS_I(inode)->alloc_blocks,
419                                              start, len);
420                         if (res == -ENOSPC)
421                                 goto insert_extent;
422                 }
423                 if (!res) {
424                         hfs_dump_extent(HFS_I(inode)->first_extents);
425                         HFS_I(inode)->first_blocks += len;
426                 }
427         } else {
428                 res = hfs_add_extent(HFS_I(inode)->cached_extents,
429                                      HFS_I(inode)->alloc_blocks -
430                                      HFS_I(inode)->cached_start,
431                                      start, len);
432                 if (!res) {
433                         hfs_dump_extent(HFS_I(inode)->cached_extents);
434                         HFS_I(inode)->flags |= HFS_FLG_EXT_DIRTY;
435                         HFS_I(inode)->cached_blocks += len;
436                 } else if (res == -ENOSPC)
437                         goto insert_extent;
438         }
439 out:
440         mutex_unlock(&HFS_I(inode)->extents_lock);
441         if (!res) {
442                 HFS_I(inode)->alloc_blocks += len;
443                 mark_inode_dirty(inode);
444                 if (inode->i_ino < HFS_FIRSTUSER_CNID)
445                         set_bit(HFS_FLG_ALT_MDB_DIRTY, &HFS_SB(sb)->flags);
446                 set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
447                 hfs_mark_mdb_dirty(sb);
448         }
449         return res;
450
451 insert_extent:
452         dprint(DBG_EXTENT, "insert new extent\n");
453         res = hfs_ext_write_extent(inode);
454         if (res)
455                 goto out;
456
457         memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec));
458         HFS_I(inode)->cached_extents[0].block = cpu_to_be16(start);
459         HFS_I(inode)->cached_extents[0].count = cpu_to_be16(len);
460         hfs_dump_extent(HFS_I(inode)->cached_extents);
461         HFS_I(inode)->flags |= HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW;
462         HFS_I(inode)->cached_start = HFS_I(inode)->alloc_blocks;
463         HFS_I(inode)->cached_blocks = len;
464
465         res = 0;
466         goto out;
467 }
468
469 void hfs_file_truncate(struct inode *inode)
470 {
471         struct super_block *sb = inode->i_sb;
472         struct hfs_find_data fd;
473         u16 blk_cnt, alloc_cnt, start;
474         u32 size;
475         int res;
476
477         dprint(DBG_INODE, "truncate: %lu, %Lu -> %Lu\n", inode->i_ino,
478                (long long)HFS_I(inode)->phys_size, inode->i_size);
479         if (inode->i_size > HFS_I(inode)->phys_size) {
480                 struct address_space *mapping = inode->i_mapping;
481                 void *fsdata;
482                 struct page *page;
483
484                 /* XXX: Can use generic_cont_expand? */
485                 size = inode->i_size - 1;
486                 res = pagecache_write_begin(NULL, mapping, size+1, 0,
487                                 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
488                 if (!res) {
489                         res = pagecache_write_end(NULL, mapping, size+1, 0, 0,
490                                         page, fsdata);
491                 }
492                 if (res)
493                         inode->i_size = HFS_I(inode)->phys_size;
494                 return;
495         } else if (inode->i_size == HFS_I(inode)->phys_size)
496                 return;
497         size = inode->i_size + HFS_SB(sb)->alloc_blksz - 1;
498         blk_cnt = size / HFS_SB(sb)->alloc_blksz;
499         alloc_cnt = HFS_I(inode)->alloc_blocks;
500         if (blk_cnt == alloc_cnt)
501                 goto out;
502
503         mutex_lock(&HFS_I(inode)->extents_lock);
504         res = hfs_find_init(HFS_SB(sb)->ext_tree, &fd);
505         if (res) {
506                 mutex_unlock(&HFS_I(inode)->extents_lock);
507                 /* XXX: We lack error handling of hfs_file_truncate() */
508                 return;
509         }
510         while (1) {
511                 if (alloc_cnt == HFS_I(inode)->first_blocks) {
512                         hfs_free_extents(sb, HFS_I(inode)->first_extents,
513                                          alloc_cnt, alloc_cnt - blk_cnt);
514                         hfs_dump_extent(HFS_I(inode)->first_extents);
515                         HFS_I(inode)->first_blocks = blk_cnt;
516                         break;
517                 }
518                 res = __hfs_ext_cache_extent(&fd, inode, alloc_cnt);
519                 if (res)
520                         break;
521                 start = HFS_I(inode)->cached_start;
522                 hfs_free_extents(sb, HFS_I(inode)->cached_extents,
523                                  alloc_cnt - start, alloc_cnt - blk_cnt);
524                 hfs_dump_extent(HFS_I(inode)->cached_extents);
525                 if (blk_cnt > start) {
526                         HFS_I(inode)->flags |= HFS_FLG_EXT_DIRTY;
527                         break;
528                 }
529                 alloc_cnt = start;
530                 HFS_I(inode)->cached_start = HFS_I(inode)->cached_blocks = 0;
531                 HFS_I(inode)->flags &= ~(HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW);
532                 hfs_brec_remove(&fd);
533         }
534         hfs_find_exit(&fd);
535         mutex_unlock(&HFS_I(inode)->extents_lock);
536
537         HFS_I(inode)->alloc_blocks = blk_cnt;
538 out:
539         HFS_I(inode)->phys_size = inode->i_size;
540         HFS_I(inode)->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
541         inode_set_bytes(inode, HFS_I(inode)->fs_blocks << sb->s_blocksize_bits);
542         mark_inode_dirty(inode);
543 }