revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / reiser4 / plugin / item / blackbox.c
blobf13ff64572c5a328f10638146ae787417422cc9b
1 /* Copyright 2003 by Hans Reiser, licensing governed by
2 * reiser4/README */
4 /* Black box item implementation */
6 #include "../../forward.h"
7 #include "../../debug.h"
8 #include "../../dformat.h"
9 #include "../../kassign.h"
10 #include "../../coord.h"
11 #include "../../tree.h"
12 #include "../../lock.h"
14 #include "blackbox.h"
15 #include "item.h"
16 #include "../plugin.h"
18 int
19 store_black_box(reiser4_tree * tree,
20 const reiser4_key * key, void *data, int length)
22 int result;
23 reiser4_item_data idata;
24 coord_t coord;
25 lock_handle lh;
27 memset(&idata, 0, sizeof idata);
29 idata.data = data;
30 idata.user = 0;
31 idata.length = length;
32 idata.iplug = item_plugin_by_id(BLACK_BOX_ID);
34 init_lh(&lh);
35 result = insert_by_key(tree, key,
36 &idata, &coord, &lh, LEAF_LEVEL, CBK_UNIQUE);
38 assert("nikita-3413",
39 ergo(result == 0,
40 WITH_COORD(&coord,
41 item_length_by_coord(&coord) == length)));
43 done_lh(&lh);
44 return result;
47 int
48 load_black_box(reiser4_tree * tree,
49 reiser4_key * key, void *data, int length, int exact)
51 int result;
52 coord_t coord;
53 lock_handle lh;
55 init_lh(&lh);
56 result = coord_by_key(tree, key,
57 &coord, &lh, ZNODE_READ_LOCK,
58 exact ? FIND_EXACT : FIND_MAX_NOT_MORE_THAN,
59 LEAF_LEVEL, LEAF_LEVEL, CBK_UNIQUE, NULL);
61 if (result == 0) {
62 int ilen;
64 result = zload(coord.node);
65 if (result == 0) {
66 ilen = item_length_by_coord(&coord);
67 if (ilen <= length) {
68 memcpy(data, item_body_by_coord(&coord), ilen);
69 unit_key_by_coord(&coord, key);
70 } else if (exact) {
72 * item is larger than buffer provided by the
73 * user. Only issue a warning if @exact is
74 * set. If @exact is false, we are iterating
75 * over all safe-links and here we are reaching
76 * the end of the iteration.
78 warning("nikita-3415",
79 "Wrong black box length: %i > %i",
80 ilen, length);
81 result = RETERR(-EIO);
83 zrelse(coord.node);
87 done_lh(&lh);
88 return result;
92 int
93 update_black_box(reiser4_tree * tree,
94 const reiser4_key * key, void *data, int length)
96 int result;
97 coord_t coord;
98 lock_handle lh;
100 init_lh(&lh);
101 result = coord_by_key(tree, key,
102 &coord, &lh, ZNODE_READ_LOCK,
103 FIND_EXACT,
104 LEAF_LEVEL, LEAF_LEVEL, CBK_UNIQUE, NULL);
105 if (result == 0) {
106 int ilen;
108 result = zload(coord.node);
109 if (result == 0) {
110 ilen = item_length_by_coord(&coord);
111 if (length <= ilen) {
112 memcpy(item_body_by_coord(&coord), data,
113 length);
114 } else {
115 warning("nikita-3437",
116 "Wrong black box length: %i < %i",
117 ilen, length);
118 result = RETERR(-EIO);
120 zrelse(coord.node);
124 done_lh(&lh);
125 return result;
129 int kill_black_box(reiser4_tree * tree, const reiser4_key * key)
131 return reiser4_cut_tree(tree, key, key, NULL, 1);
134 /* Make Linus happy.
135 Local variables:
136 c-indentation-style: "K&R"
137 mode-name: "LC"
138 c-basic-offset: 8
139 tab-width: 8
140 fill-column: 120
141 End: