/*
* linux/fs/nfs/blocklayout/blocklayout.h
*
* Module for the NFSv4.1 pNFS block layout driver.
*
* Copyright (c) 2006 The Regents of the University of Michigan.
* All rights reserved.
*
* Andy Adamson <andros@citi.umich.edu>
* Fred Isaman <iisaman@umich.edu>
*
* permission is granted to use, copy, create derivative works and
* redistribute this software and such derivative works for any purpose,
* so long as the name of the university of michigan is not used in
* any advertising or publicity pertaining to the use or distribution
* of this software without specific, written prior authorization. if
* the above copyright notice or any other identification of the
* university of michigan is included in any copy of any portion of
* this software, then the disclaimer below must also be included.
*
* this software is provided as is, without representation from the
* university of michigan as to its fitness for any purpose, and without
* warranty by the university of michigan of any kind, either express
* or implied, including without limitation the implied warranties of
* merchantability and fitness for a particular purpose. the regents
* of the university of michigan shall not be liable for any damages,
* including special, indirect, incidental, or consequential damages,
* with respect to any claim arising out or in connection with the use
* of the software, even if it has been or is hereafter advised of the
* possibility of such damages.
*/
#include "blocklayout.h"
#define NFSDBG_FACILITY NFSDBG_PNFS_LD
/* Bit numbers */
#define EXTENT_INITIALIZED 0
#define EXTENT_WRITTEN 1
#define EXTENT_IN_COMMIT 2
#define INTERNAL_EXISTS MY_MAX_TAGS
#define INTERNAL_MASK ((1 << INTERNAL_EXISTS) - 1)
/* Returns largest t<=s s.t. t%base==0 */
static inline sector_t normalize(sector_t s, int base)
{
sector_t tmp = s; /* Since do_div modifies its argument */
return s - do_div(tmp, base);
}
static inline sector_t normalize_up(sector_t s, int base)
{
return normalize(s + base - 1, base);
}
/* Complete stub using list while determine API wanted */
/* Returns tags, or negative */
static int32_t _find_entry(struct my_tree *tree, u64 s)
{
struct pnfs_inval_tracking *pos;
dprintk("%s(%llu) enter\n", __func__, s);
list_for_each_entry_reverse(pos, &tree->mtt_stub, it_link) {
if (pos->it_sector > s)
continue;
else if (pos->it_sector == s)
return pos->it_tags & INTERNAL_MASK;
else
break;
}
return -ENOENT;
}
static inline
int _has_tag(struct my_tree *tree, u64 s, int32_t tag)
{
int32_t tags;
dprintk("%s(%llu, %i) enter\n", __func__, s, tag);
s = normalize(s, tree->mtt_step_size);
tags = _find_entry(tree, s);
if ((tags < 0) || !(tags & (1 << tag)))
return 0;
else
return 1;
}
/* Creates entry with tag, or if entry already exists, unions tag to it.
* If storage is not NULL, newly created entry will use it.
* Returns number of entries added, or negative on error.
*/
static int _add_entry(struct my_tree *tree, u64 s, int32_t tag,
struct pnfs_inval_tracking *storage)
{
int found = 0;
struct pnfs_inval_tracking *pos;
dprintk("%s(%llu, %i, %p) enter\n", __func__, s, tag, storage);
list_for_each_entry_reverse(pos, &tree->mtt_stub, it_link) {
if (pos->it_sector > s)
continue;
else if (pos->it_sector == s) {
found = 1;
break;
} else
break;
}
if (found) {
pos->it_tags |= (1 << tag);
return 0;
} else {
struct pnfs_inval_tracking *new;
if (storage)
new = storage;
else {
new = kmalloc(sizeof(*new), GFP_NOFS);
if (!new)
return -ENOMEM;
}
new->it_sector = s;
new->it_tags = (1 << tag);
list_add(&new->it_link, &pos->it_link);
return 1;
}
}
/* XXXX Really want option to not create */
/* Over range, unions tag with existing entries, else creates entry with tag */
static int _set_range(struct my_tree *tree, int32_t tag, u64 s, u64 length)
{
u64 i;
dprintk("%s(%i, %llu, %llu) enter\n", __func__, tag, s, length);
for (i = normalize(s, tree->mtt_step_size); i < s + length;
i += tree->mtt_step_size)
if (_add_entry(tree, i, tag, NULL))
return -ENOMEM;
return 0;
}
/* Ensure that future operations on given range of tree will not malloc */
static int _preload_range(struct my_tree *tree, u64 offset, u64 length)
{
u64 start, end, s;
int count, i, used = 0, status = -ENOMEM;
struct pnfs_inval_tracking **storage;
dprintk("%s(%llu, %llu) enter\n", __func__, offset, length);
start = normalize(offset, tree->mtt_step_size);
end = normalize_up(offset + length, tree->mtt_step_size);
count = (int)(end - start) / (int)tree->mtt_step_size;