/* -------------------------------------------------------------------------
* File: fs/jffs2/xattr.c
* XATTR support on JFFS2 FileSystem
*
* Implemented by KaiGai Kohei <kaigai@ak.jp.nec.com>
* Copyright (C) 2006 NEC Corporation
*
* For licensing information, see the file 'LICENCE' in the jffs2 directory.
* ------------------------------------------------------------------------- */
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/time.h>
#include <linux/pagemap.h>
#include <linux/highmem.h>
#include <linux/crc32.h>
#include <linux/jffs2.h>
#include <linux/xattr.h>
#include <linux/mtd/mtd.h>
#include "nodelist.h"
/* -------- xdatum related functions ----------------
* xattr_datum_hashkey(xprefix, xname, xvalue, xsize)
* is used to calcurate xdatum hashkey. The reminder of hashkey into XATTRINDEX_HASHSIZE is
* the index of the xattr name/value pair cache (c->xattrindex).
* unload_xattr_datum(c, xd)
* is used to release xattr name/value pair and detach from c->xattrindex.
* reclaim_xattr_datum(c)
* is used to reclaim xattr name/value pairs on the xattr name/value pair cache when
* memory usage by cache is over c->xdatum_mem_threshold. Currentry, this threshold
* is hard coded as 32KiB.
* delete_xattr_datum_node(c, xd)
* is used to delete a jffs2 node is dominated by xdatum. When EBS(Erase Block Summary) is
* enabled, it overwrites the obsolete node by myself.
* delete_xattr_datum(c, xd)
* is used to delete jffs2_xattr_datum object. It must be called with 0-value of reference
* counter. (It means how many jffs2_xattr_ref object refers this xdatum.)
* do_verify_xattr_datum(c, xd)
* is used to load the xdatum informations without name/value pair from the medium.
* It's necessary once, because those informations are not collected during mounting
* process when EBS is enabled.
* 0 will be returned, if success. An negative return value means recoverable error, and
* positive return value means unrecoverable error. Thus, caller must remove this xdatum
* and xref when it returned positive value.
* do_load_xattr_datum(c, xd)
* is used to load name/value pair from the medium.
* The meanings of return value is same as do_verify_xattr_datum().
* load_xattr_datum(c, xd)
* is used to be as a wrapper of do_verify_xattr_datum() and do_load_xattr_datum().
* If xd need to call do_verify_xattr_datum() at first, it's called before calling
* do_load_xattr_datum(). The meanings of return value is same as do_verify_xattr_datum().
* save_xattr_datum(c, xd, phys_ofs)
* is used to write xdatum to medium. xd->version will be incremented.
* create_xattr_datum(c, xprefix, xname, xvalue, xsize, phys_ofs)
* is used to create new xdatum and write to medium.
* -------------------------------------------------- */
static uint32_t xattr_datum_hashkey(int xprefix, const char *xname, const char *xvalue, int xsize)
{
int name_len = strlen(xname);
return crc32(xprefix, xname, name_len) ^ crc32(xprefix, xvalue, xsize);
}
static void unload_xattr_datum(struct jffs2_sb_info *c, struct jffs2_xattr_datum *xd)
{
/* must be called under down_write(xattr_sem) */
D1(dbg_xattr("%s: xid=%u, version=%u\n", __FUNCTION__, xd->xid, xd->version));
if (xd->xname) {
c->xdatum_mem_usage -= (xd->name_len + 1 + xd->value_len);
kfree(xd->xname);
}
list_del_init(&xd->xindex);
xd->hashkey = 0;
xd->xname = NULL;
xd->xvalue = NULL;
}
static void reclaim_xattr_datum(struct jffs2_sb_info *c)
{
/* must be called under down_write(xattr_sem) */
struct jffs2_xattr_datum *xd, *_xd;
uint32_t target, before;
static int index = 0;
int count;
if (c->xdatum_mem_threshold > c->xdatum_mem_usage)
return;
before = c->xdatum_mem_usage;
target = c->xdatum_mem_usage * 4 / 5; /* 20% reduction */
for (count = 0; count < XATTRINDEX_HASHSIZE; count++) {
list_for_each_entry_safe(xd, _xd, &c->xattrindex[index], xindex