xattr.c (cf1d6c763fbcb115263114302485ad17e7933d87) xattr.c (0c044f0b24b9128ba8c297149d88bd81f2e36af3)
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
6 * Copyright (C) 2008 Oracle. All rights reserved.
7 *
8 * CREDITS:

--- 38 unchanged lines hidden (view full) ---

47#include "symlink.h"
48#include "sysfile.h"
49#include "inode.h"
50#include "journal.h"
51#include "ocfs2_fs.h"
52#include "suballoc.h"
53#include "uptodate.h"
54#include "buffer_head_io.h"
1/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * xattr.c
5 *
6 * Copyright (C) 2008 Oracle. All rights reserved.
7 *
8 * CREDITS:

--- 38 unchanged lines hidden (view full) ---

47#include "symlink.h"
48#include "sysfile.h"
49#include "inode.h"
50#include "journal.h"
51#include "ocfs2_fs.h"
52#include "suballoc.h"
53#include "uptodate.h"
54#include "buffer_head_io.h"
55#include "super.h"
55#include "xattr.h"
56
57
58struct ocfs2_xattr_def_value_root {
59 struct ocfs2_xattr_value_root xv;
60 struct ocfs2_extent_rec er;
61};
62
56#include "xattr.h"
57
58
59struct ocfs2_xattr_def_value_root {
60 struct ocfs2_xattr_value_root xv;
61 struct ocfs2_extent_rec er;
62};
63
64struct ocfs2_xattr_bucket {
65 struct buffer_head *bhs[OCFS2_XATTR_MAX_BLOCKS_PER_BUCKET];
66 struct ocfs2_xattr_header *xh;
67};
68
63#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
64#define OCFS2_XATTR_INLINE_SIZE 80
65
66static struct ocfs2_xattr_def_value_root def_xv = {
67 .xv.xr_list.l_count = cpu_to_le16(1),
68};
69
70struct xattr_handler *ocfs2_xattr_handlers[] = {

--- 23 unchanged lines hidden (view full) ---

94 struct buffer_head *xattr_bh;
95 struct ocfs2_xattr_header *header;
96 void *base;
97 void *end;
98 struct ocfs2_xattr_entry *here;
99 int not_found;
100};
101
69#define OCFS2_XATTR_ROOT_SIZE (sizeof(struct ocfs2_xattr_def_value_root))
70#define OCFS2_XATTR_INLINE_SIZE 80
71
72static struct ocfs2_xattr_def_value_root def_xv = {
73 .xv.xr_list.l_count = cpu_to_le16(1),
74};
75
76struct xattr_handler *ocfs2_xattr_handlers[] = {

--- 23 unchanged lines hidden (view full) ---

100 struct buffer_head *xattr_bh;
101 struct ocfs2_xattr_header *header;
102 void *base;
103 void *end;
104 struct ocfs2_xattr_entry *here;
105 int not_found;
106};
107
108static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
109 struct ocfs2_xattr_tree_root *xt,
110 char *buffer,
111 size_t buffer_size);
112
102static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
103{
104 struct xattr_handler *handler = NULL;
105
106 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
107 handler = ocfs2_xattr_handler_map[name_index];
108
109 return handler;

--- 368 unchanged lines hidden (view full) ---

478}
479
480static int ocfs2_xattr_block_list(struct inode *inode,
481 struct ocfs2_dinode *di,
482 char *buffer,
483 size_t buffer_size)
484{
485 struct buffer_head *blk_bh = NULL;
113static inline struct xattr_handler *ocfs2_xattr_handler(int name_index)
114{
115 struct xattr_handler *handler = NULL;
116
117 if (name_index > 0 && name_index < OCFS2_XATTR_MAX)
118 handler = ocfs2_xattr_handler_map[name_index];
119
120 return handler;

--- 368 unchanged lines hidden (view full) ---

489}
490
491static int ocfs2_xattr_block_list(struct inode *inode,
492 struct ocfs2_dinode *di,
493 char *buffer,
494 size_t buffer_size)
495{
496 struct buffer_head *blk_bh = NULL;
486 struct ocfs2_xattr_header *header = NULL;
497 struct ocfs2_xattr_block *xb;
487 int ret = 0;
488
489 if (!di->i_xattr_loc)
490 return ret;
491
492 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
493 le64_to_cpu(di->i_xattr_loc),
494 &blk_bh, OCFS2_BH_CACHED, inode);
495 if (ret < 0) {
496 mlog_errno(ret);
497 return ret;
498 }
499 /*Verify the signature of xattr block*/
500 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
501 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
502 ret = -EFAULT;
503 goto cleanup;
504 }
505
498 int ret = 0;
499
500 if (!di->i_xattr_loc)
501 return ret;
502
503 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
504 le64_to_cpu(di->i_xattr_loc),
505 &blk_bh, OCFS2_BH_CACHED, inode);
506 if (ret < 0) {
507 mlog_errno(ret);
508 return ret;
509 }
510 /*Verify the signature of xattr block*/
511 if (memcmp((void *)blk_bh->b_data, OCFS2_XATTR_BLOCK_SIGNATURE,
512 strlen(OCFS2_XATTR_BLOCK_SIGNATURE))) {
513 ret = -EFAULT;
514 goto cleanup;
515 }
516
506 header = &((struct ocfs2_xattr_block *)blk_bh->b_data)->
507 xb_attrs.xb_header;
517 xb = (struct ocfs2_xattr_block *)blk_bh->b_data;
508
518
509 ret = ocfs2_xattr_list_entries(inode, header, buffer, buffer_size);
519 if (!(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED)) {
520 struct ocfs2_xattr_header *header = &xb->xb_attrs.xb_header;
521 ret = ocfs2_xattr_list_entries(inode, header,
522 buffer, buffer_size);
523 } else {
524 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
525 ret = ocfs2_xattr_tree_list_index_block(inode, xt,
526 buffer, buffer_size);
527 }
510cleanup:
511 brelse(blk_bh);
512
513 return ret;
514}
515
516ssize_t ocfs2_listxattr(struct dentry *dentry,
517 char *buffer,

--- 1400 unchanged lines hidden (view full) ---

1918 up_write(&OCFS2_I(inode)->ip_xattr_sem);
1919 ocfs2_inode_unlock(inode, 1);
1920 brelse(di_bh);
1921 brelse(xbs.xattr_bh);
1922
1923 return ret;
1924}
1925
528cleanup:
529 brelse(blk_bh);
530
531 return ret;
532}
533
534ssize_t ocfs2_listxattr(struct dentry *dentry,
535 char *buffer,

--- 1400 unchanged lines hidden (view full) ---

1936 up_write(&OCFS2_I(inode)->ip_xattr_sem);
1937 ocfs2_inode_unlock(inode, 1);
1938 brelse(di_bh);
1939 brelse(xbs.xattr_bh);
1940
1941 return ret;
1942}
1943
1944/*
1945 * Find the xattr extent rec which may contains name_hash.
1946 * e_cpos will be the first name hash of the xattr rec.
1947 * el must be the ocfs2_xattr_header.xb_attrs.xb_root.xt_list.
1948 */
1949static int ocfs2_xattr_get_rec(struct inode *inode,
1950 u32 name_hash,
1951 u64 *p_blkno,
1952 u32 *e_cpos,
1953 u32 *num_clusters,
1954 struct ocfs2_extent_list *el)
1955{
1956 int ret = 0, i;
1957 struct buffer_head *eb_bh = NULL;
1958 struct ocfs2_extent_block *eb;
1959 struct ocfs2_extent_rec *rec = NULL;
1960 u64 e_blkno = 0;
1961
1962 if (el->l_tree_depth) {
1963 ret = ocfs2_find_leaf(inode, el, name_hash, &eb_bh);
1964 if (ret) {
1965 mlog_errno(ret);
1966 goto out;
1967 }
1968
1969 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1970 el = &eb->h_list;
1971
1972 if (el->l_tree_depth) {
1973 ocfs2_error(inode->i_sb,
1974 "Inode %lu has non zero tree depth in "
1975 "xattr tree block %llu\n", inode->i_ino,
1976 (unsigned long long)eb_bh->b_blocknr);
1977 ret = -EROFS;
1978 goto out;
1979 }
1980 }
1981
1982 for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1983 rec = &el->l_recs[i];
1984
1985 if (le32_to_cpu(rec->e_cpos) <= name_hash) {
1986 e_blkno = le64_to_cpu(rec->e_blkno);
1987 break;
1988 }
1989 }
1990
1991 if (!e_blkno) {
1992 ocfs2_error(inode->i_sb, "Inode %lu has bad extent "
1993 "record (%u, %u, 0) in xattr", inode->i_ino,
1994 le32_to_cpu(rec->e_cpos),
1995 ocfs2_rec_clusters(el, rec));
1996 ret = -EROFS;
1997 goto out;
1998 }
1999
2000 *p_blkno = le64_to_cpu(rec->e_blkno);
2001 *num_clusters = le16_to_cpu(rec->e_leaf_clusters);
2002 if (e_cpos)
2003 *e_cpos = le32_to_cpu(rec->e_cpos);
2004out:
2005 brelse(eb_bh);
2006 return ret;
2007}
2008
2009typedef int (xattr_bucket_func)(struct inode *inode,
2010 struct ocfs2_xattr_bucket *bucket,
2011 void *para);
2012
2013static int ocfs2_iterate_xattr_buckets(struct inode *inode,
2014 u64 blkno,
2015 u32 clusters,
2016 xattr_bucket_func *func,
2017 void *para)
2018{
2019 int i, j, ret = 0;
2020 int blk_per_bucket = ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2021 u32 bpc = ocfs2_xattr_buckets_per_cluster(OCFS2_SB(inode->i_sb));
2022 u32 num_buckets = clusters * bpc;
2023 struct ocfs2_xattr_bucket bucket;
2024
2025 memset(&bucket, 0, sizeof(bucket));
2026
2027 mlog(0, "iterating xattr buckets in %u clusters starting from %llu\n",
2028 clusters, blkno);
2029
2030 for (i = 0; i < num_buckets; i++, blkno += blk_per_bucket) {
2031 ret = ocfs2_read_blocks(OCFS2_SB(inode->i_sb),
2032 blkno, blk_per_bucket,
2033 bucket.bhs, OCFS2_BH_CACHED, inode);
2034 if (ret) {
2035 mlog_errno(ret);
2036 goto out;
2037 }
2038
2039 bucket.xh = (struct ocfs2_xattr_header *)bucket.bhs[0]->b_data;
2040 /*
2041 * The real bucket num in this series of blocks is stored
2042 * in the 1st bucket.
2043 */
2044 if (i == 0)
2045 num_buckets = le16_to_cpu(bucket.xh->xh_num_buckets);
2046
2047 mlog(0, "iterating xattr bucket %llu\n", blkno);
2048 if (func) {
2049 ret = func(inode, &bucket, para);
2050 if (ret) {
2051 mlog_errno(ret);
2052 break;
2053 }
2054 }
2055
2056 for (j = 0; j < blk_per_bucket; j++)
2057 brelse(bucket.bhs[j]);
2058 memset(&bucket, 0, sizeof(bucket));
2059 }
2060
2061out:
2062 for (j = 0; j < blk_per_bucket; j++)
2063 brelse(bucket.bhs[j]);
2064
2065 return ret;
2066}
2067
2068struct ocfs2_xattr_tree_list {
2069 char *buffer;
2070 size_t buffer_size;
2071};
2072
2073static int ocfs2_xattr_bucket_get_name_value(struct inode *inode,
2074 struct ocfs2_xattr_header *xh,
2075 int index,
2076 int *block_off,
2077 int *new_offset)
2078{
2079 u16 name_offset;
2080
2081 if (index < 0 || index >= le16_to_cpu(xh->xh_count))
2082 return -EINVAL;
2083
2084 name_offset = le16_to_cpu(xh->xh_entries[index].xe_name_offset);
2085
2086 *block_off = name_offset >> inode->i_sb->s_blocksize_bits;
2087 *new_offset = name_offset % inode->i_sb->s_blocksize;
2088
2089 return 0;
2090}
2091
2092static int ocfs2_list_xattr_bucket(struct inode *inode,
2093 struct ocfs2_xattr_bucket *bucket,
2094 void *para)
2095{
2096 int ret = 0;
2097 struct ocfs2_xattr_tree_list *xl = (struct ocfs2_xattr_tree_list *)para;
2098 size_t size;
2099 int i, block_off, new_offset;
2100
2101 for (i = 0 ; i < le16_to_cpu(bucket->xh->xh_count); i++) {
2102 struct ocfs2_xattr_entry *entry = &bucket->xh->xh_entries[i];
2103 struct xattr_handler *handler =
2104 ocfs2_xattr_handler(ocfs2_xattr_get_type(entry));
2105
2106 if (handler) {
2107 ret = ocfs2_xattr_bucket_get_name_value(inode,
2108 bucket->xh,
2109 i,
2110 &block_off,
2111 &new_offset);
2112 if (ret)
2113 break;
2114 size = handler->list(inode, xl->buffer, xl->buffer_size,
2115 bucket->bhs[block_off]->b_data +
2116 new_offset,
2117 entry->xe_name_len);
2118 if (xl->buffer) {
2119 if (size > xl->buffer_size)
2120 return -ERANGE;
2121 xl->buffer += size;
2122 }
2123 xl->buffer_size -= size;
2124 }
2125 }
2126
2127 return ret;
2128}
2129
2130static int ocfs2_xattr_tree_list_index_block(struct inode *inode,
2131 struct ocfs2_xattr_tree_root *xt,
2132 char *buffer,
2133 size_t buffer_size)
2134{
2135 struct ocfs2_extent_list *el = &xt->xt_list;
2136 int ret = 0;
2137 u32 name_hash = UINT_MAX, e_cpos = 0, num_clusters = 0;
2138 u64 p_blkno = 0;
2139 struct ocfs2_xattr_tree_list xl = {
2140 .buffer = buffer,
2141 .buffer_size = buffer_size,
2142 };
2143
2144 if (le16_to_cpu(el->l_next_free_rec) == 0)
2145 return 0;
2146
2147 while (name_hash > 0) {
2148 ret = ocfs2_xattr_get_rec(inode, name_hash, &p_blkno,
2149 &e_cpos, &num_clusters, el);
2150 if (ret) {
2151 mlog_errno(ret);
2152 goto out;
2153 }
2154
2155 ret = ocfs2_iterate_xattr_buckets(inode, p_blkno, num_clusters,
2156 ocfs2_list_xattr_bucket,
2157 &xl);
2158 if (ret) {
2159 mlog_errno(ret);
2160 goto out;
2161 }
2162
2163 if (e_cpos == 0)
2164 break;
2165
2166 name_hash = e_cpos - 1;
2167 }
2168
2169 ret = buffer_size - xl.buffer_size;
2170out:
2171 return ret;
2172}