Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of UBIFS. |
| 3 | * |
| 4 | * Copyright (C) 2006-2008 Nokia Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | * Authors: Artem Bityutskiy (Битюцкий Артём) |
| 20 | * Adrian Hunter |
| 21 | */ |
| 22 | |
| 23 | /* |
| 24 | * This file contains miscellaneous helper functions. |
| 25 | */ |
| 26 | |
| 27 | #ifndef __UBIFS_MISC_H__ |
| 28 | #define __UBIFS_MISC_H__ |
| 29 | |
| 30 | /** |
| 31 | * ubifs_zn_dirty - check if znode is dirty. |
| 32 | * @znode: znode to check |
| 33 | * |
| 34 | * This helper function returns %1 if @znode is dirty and %0 otherwise. |
| 35 | */ |
| 36 | static inline int ubifs_zn_dirty(const struct ubifs_znode *znode) |
| 37 | { |
| 38 | return !!test_bit(DIRTY_ZNODE, &znode->flags); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * ubifs_wake_up_bgt - wake up background thread. |
| 43 | * @c: UBIFS file-system description object |
| 44 | */ |
| 45 | static inline void ubifs_wake_up_bgt(struct ubifs_info *c) |
| 46 | { |
| 47 | if (c->bgt && !c->need_bgt) { |
| 48 | c->need_bgt = 1; |
| 49 | wake_up_process(c->bgt); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * ubifs_tnc_find_child - find next child in znode. |
| 55 | * @znode: znode to search at |
| 56 | * @start: the zbranch index to start at |
| 57 | * |
| 58 | * This helper function looks for znode child starting at index @start. Returns |
| 59 | * the child or %NULL if no children were found. |
| 60 | */ |
| 61 | static inline struct ubifs_znode * |
| 62 | ubifs_tnc_find_child(struct ubifs_znode *znode, int start) |
| 63 | { |
| 64 | while (start < znode->child_cnt) { |
| 65 | if (znode->zbranch[start].znode) |
| 66 | return znode->zbranch[start].znode; |
| 67 | start += 1; |
| 68 | } |
| 69 | |
| 70 | return NULL; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * ubifs_inode - get UBIFS inode information by VFS 'struct inode' object. |
| 75 | * @inode: the VFS 'struct inode' pointer |
| 76 | */ |
| 77 | static inline struct ubifs_inode *ubifs_inode(const struct inode *inode) |
| 78 | { |
| 79 | return container_of(inode, struct ubifs_inode, vfs_inode); |
| 80 | } |
| 81 | |
| 82 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 83 | * ubifs_compr_present - check if compressor was compiled in. |
| 84 | * @compr_type: compressor type to check |
| 85 | * |
| 86 | * This function returns %1 of compressor of type @compr_type is present, and |
| 87 | * %0 if not. |
| 88 | */ |
| 89 | static inline int ubifs_compr_present(int compr_type) |
| 90 | { |
| 91 | ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT); |
| 92 | return !!ubifs_compressors[compr_type]->capi_name; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * ubifs_compr_name - get compressor name string by its type. |
| 97 | * @compr_type: compressor type |
| 98 | * |
| 99 | * This function returns compressor type string. |
| 100 | */ |
| 101 | static inline const char *ubifs_compr_name(int compr_type) |
| 102 | { |
| 103 | ubifs_assert(compr_type >= 0 && compr_type < UBIFS_COMPR_TYPES_CNT); |
| 104 | return ubifs_compressors[compr_type]->name; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * ubifs_wbuf_sync - synchronize write-buffer. |
| 109 | * @wbuf: write-buffer to synchronize |
| 110 | * |
| 111 | * This is the same as as 'ubifs_wbuf_sync_nolock()' but it does not assume |
| 112 | * that the write-buffer is already locked. |
| 113 | */ |
| 114 | static inline int ubifs_wbuf_sync(struct ubifs_wbuf *wbuf) |
| 115 | { |
| 116 | int err; |
| 117 | |
| 118 | mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); |
| 119 | err = ubifs_wbuf_sync_nolock(wbuf); |
| 120 | mutex_unlock(&wbuf->io_mutex); |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * ubifs_leb_unmap - unmap an LEB. |
| 126 | * @c: UBIFS file-system description object |
| 127 | * @lnum: LEB number to unmap |
| 128 | * |
| 129 | * This function returns %0 on success and a negative error code on failure. |
| 130 | */ |
| 131 | static inline int ubifs_leb_unmap(const struct ubifs_info *c, int lnum) |
| 132 | { |
| 133 | int err; |
| 134 | |
Artem Bityutskiy | 2ef1329 | 2010-09-19 18:34:26 +0300 | [diff] [blame] | 135 | ubifs_assert(!c->ro_media && !c->ro_mount); |
Artem Bityutskiy | 2680d72 | 2010-09-17 16:44:28 +0300 | [diff] [blame] | 136 | if (c->ro_error) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 137 | return -EROFS; |
| 138 | err = ubi_leb_unmap(c->ubi, lnum); |
| 139 | if (err) { |
| 140 | ubifs_err("unmap LEB %d failed, error %d", lnum, err); |
| 141 | return err; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * ubifs_leb_write - write to a LEB. |
| 149 | * @c: UBIFS file-system description object |
| 150 | * @lnum: LEB number to write |
| 151 | * @buf: buffer to write from |
| 152 | * @offs: offset within LEB to write to |
| 153 | * @len: length to write |
| 154 | * @dtype: data type |
| 155 | * |
| 156 | * This function returns %0 on success and a negative error code on failure. |
| 157 | */ |
| 158 | static inline int ubifs_leb_write(const struct ubifs_info *c, int lnum, |
| 159 | const void *buf, int offs, int len, int dtype) |
| 160 | { |
| 161 | int err; |
| 162 | |
Artem Bityutskiy | 2ef1329 | 2010-09-19 18:34:26 +0300 | [diff] [blame] | 163 | ubifs_assert(!c->ro_media && !c->ro_mount); |
Artem Bityutskiy | 2680d72 | 2010-09-17 16:44:28 +0300 | [diff] [blame] | 164 | if (c->ro_error) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 165 | return -EROFS; |
| 166 | err = ubi_leb_write(c->ubi, lnum, buf, offs, len, dtype); |
| 167 | if (err) { |
| 168 | ubifs_err("writing %d bytes at %d:%d, error %d", |
| 169 | len, lnum, offs, err); |
| 170 | return err; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * ubifs_leb_change - atomic LEB change. |
| 178 | * @c: UBIFS file-system description object |
| 179 | * @lnum: LEB number to write |
| 180 | * @buf: buffer to write from |
| 181 | * @len: length to write |
| 182 | * @dtype: data type |
| 183 | * |
| 184 | * This function returns %0 on success and a negative error code on failure. |
| 185 | */ |
| 186 | static inline int ubifs_leb_change(const struct ubifs_info *c, int lnum, |
| 187 | const void *buf, int len, int dtype) |
| 188 | { |
| 189 | int err; |
| 190 | |
Artem Bityutskiy | 2ef1329 | 2010-09-19 18:34:26 +0300 | [diff] [blame] | 191 | ubifs_assert(!c->ro_media && !c->ro_mount); |
Artem Bityutskiy | 2680d72 | 2010-09-17 16:44:28 +0300 | [diff] [blame] | 192 | if (c->ro_error) |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 193 | return -EROFS; |
| 194 | err = ubi_leb_change(c->ubi, lnum, buf, len, dtype); |
| 195 | if (err) { |
| 196 | ubifs_err("changing %d bytes in LEB %d, error %d", |
| 197 | len, lnum, err); |
| 198 | return err; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * ubifs_encode_dev - encode device node IDs. |
| 206 | * @dev: UBIFS device node information |
| 207 | * @rdev: device IDs to encode |
| 208 | * |
| 209 | * This is a helper function which encodes major/minor numbers of a device node |
| 210 | * into UBIFS device node description. We use standard Linux "new" and "huge" |
| 211 | * encodings. |
| 212 | */ |
| 213 | static inline int ubifs_encode_dev(union ubifs_dev_desc *dev, dev_t rdev) |
| 214 | { |
| 215 | if (new_valid_dev(rdev)) { |
| 216 | dev->new = cpu_to_le32(new_encode_dev(rdev)); |
| 217 | return sizeof(dev->new); |
| 218 | } else { |
| 219 | dev->huge = cpu_to_le64(huge_encode_dev(rdev)); |
| 220 | return sizeof(dev->huge); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * ubifs_add_dirt - add dirty space to LEB properties. |
| 226 | * @c: the UBIFS file-system description object |
| 227 | * @lnum: LEB to add dirty space for |
| 228 | * @dirty: dirty space to add |
| 229 | * |
| 230 | * This is a helper function which increased amount of dirty LEB space. Returns |
| 231 | * zero in case of success and a negative error code in case of failure. |
| 232 | */ |
| 233 | static inline int ubifs_add_dirt(struct ubifs_info *c, int lnum, int dirty) |
| 234 | { |
| 235 | return ubifs_update_one_lp(c, lnum, LPROPS_NC, dirty, 0, 0); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * ubifs_return_leb - return LEB to lprops. |
| 240 | * @c: the UBIFS file-system description object |
| 241 | * @lnum: LEB to return |
| 242 | * |
| 243 | * This helper function cleans the "taken" flag of a logical eraseblock in the |
| 244 | * lprops. Returns zero in case of success and a negative error code in case of |
| 245 | * failure. |
| 246 | */ |
| 247 | static inline int ubifs_return_leb(struct ubifs_info *c, int lnum) |
| 248 | { |
| 249 | return ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0, |
| 250 | LPROPS_TAKEN, 0); |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * ubifs_idx_node_sz - return index node size. |
| 255 | * @c: the UBIFS file-system description object |
| 256 | * @child_cnt: number of children of this index node |
| 257 | */ |
| 258 | static inline int ubifs_idx_node_sz(const struct ubifs_info *c, int child_cnt) |
| 259 | { |
| 260 | return UBIFS_IDX_NODE_SZ + (UBIFS_BRANCH_SZ + c->key_len) * child_cnt; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * ubifs_idx_branch - return pointer to an index branch. |
| 265 | * @c: the UBIFS file-system description object |
| 266 | * @idx: index node |
| 267 | * @bnum: branch number |
| 268 | */ |
| 269 | static inline |
| 270 | struct ubifs_branch *ubifs_idx_branch(const struct ubifs_info *c, |
| 271 | const struct ubifs_idx_node *idx, |
| 272 | int bnum) |
| 273 | { |
| 274 | return (struct ubifs_branch *)((void *)idx->branches + |
| 275 | (UBIFS_BRANCH_SZ + c->key_len) * bnum); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * ubifs_idx_key - return pointer to an index key. |
| 280 | * @c: the UBIFS file-system description object |
| 281 | * @idx: index node |
| 282 | */ |
| 283 | static inline void *ubifs_idx_key(const struct ubifs_info *c, |
| 284 | const struct ubifs_idx_node *idx) |
| 285 | { |
| 286 | return (void *)((struct ubifs_branch *)idx->branches)->key; |
| 287 | } |
| 288 | |
| 289 | /** |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 290 | * ubifs_current_time - round current time to time granularity. |
| 291 | * @inode: inode |
| 292 | */ |
| 293 | static inline struct timespec ubifs_current_time(struct inode *inode) |
| 294 | { |
| 295 | return (inode->i_sb->s_time_gran < NSEC_PER_SEC) ? |
| 296 | current_fs_time(inode->i_sb) : CURRENT_TIME_SEC; |
| 297 | } |
| 298 | |
Adrian Hunter | 601c0bc | 2008-08-22 14:23:35 +0300 | [diff] [blame] | 299 | /** |
| 300 | * ubifs_tnc_lookup - look up a file-system node. |
| 301 | * @c: UBIFS file-system description object |
| 302 | * @key: node key to lookup |
| 303 | * @node: the node is returned here |
| 304 | * |
| 305 | * This function look up and reads node with key @key. The caller has to make |
| 306 | * sure the @node buffer is large enough to fit the node. Returns zero in case |
| 307 | * of success, %-ENOENT if the node was not found, and a negative error code in |
| 308 | * case of failure. |
| 309 | */ |
| 310 | static inline int ubifs_tnc_lookup(struct ubifs_info *c, |
| 311 | const union ubifs_key *key, void *node) |
| 312 | { |
| 313 | return ubifs_tnc_locate(c, key, node, NULL, NULL); |
| 314 | } |
| 315 | |
Artem Bityutskiy | 746103a | 2008-08-27 12:50:57 +0300 | [diff] [blame] | 316 | /** |
| 317 | * ubifs_get_lprops - get reference to LEB properties. |
| 318 | * @c: the UBIFS file-system description object |
| 319 | * |
| 320 | * This function locks lprops. Lprops have to be unlocked by |
| 321 | * 'ubifs_release_lprops()'. |
| 322 | */ |
| 323 | static inline void ubifs_get_lprops(struct ubifs_info *c) |
| 324 | { |
| 325 | mutex_lock(&c->lp_mutex); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * ubifs_release_lprops - release lprops lock. |
| 330 | * @c: the UBIFS file-system description object |
| 331 | * |
| 332 | * This function has to be called after each 'ubifs_get_lprops()' call to |
| 333 | * unlock lprops. |
| 334 | */ |
| 335 | static inline void ubifs_release_lprops(struct ubifs_info *c) |
| 336 | { |
| 337 | ubifs_assert(mutex_is_locked(&c->lp_mutex)); |
| 338 | ubifs_assert(c->lst.empty_lebs >= 0 && |
| 339 | c->lst.empty_lebs <= c->main_lebs); |
| 340 | mutex_unlock(&c->lp_mutex); |
| 341 | } |
| 342 | |
Artem Bityutskiy | e11602e | 2011-05-06 17:52:32 +0300 | [diff] [blame] | 343 | /** |
| 344 | * ubifs_next_log_lnum - switch to the next log LEB. |
| 345 | * @c: UBIFS file-system description object |
| 346 | * @lnum: current log LEB |
| 347 | * |
| 348 | * This helper function returns the log LEB number which goes next after LEB |
| 349 | * 'lnum'. |
| 350 | */ |
| 351 | static inline int ubifs_next_log_lnum(const struct ubifs_info *c, int lnum) |
| 352 | { |
| 353 | lnum += 1; |
| 354 | if (lnum > c->log_last) |
| 355 | lnum = UBIFS_LOG_LNUM; |
| 356 | |
| 357 | return lnum; |
| 358 | } |
| 359 | |
Artem Bityutskiy | 1e51764 | 2008-07-14 19:08:37 +0300 | [diff] [blame] | 360 | #endif /* __UBIFS_MISC_H__ */ |