aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ocfs2/xattr.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ocfs2/xattr.c')
-rw-r--r--fs/ocfs2/xattr.c2264
1 files changed, 1216 insertions, 1048 deletions
diff --git a/fs/ocfs2/xattr.c b/fs/ocfs2/xattr.c
index fe3419068df2..3e7773089b96 100644
--- a/fs/ocfs2/xattr.c
+++ b/fs/ocfs2/xattr.c
@@ -98,10 +98,8 @@ static struct ocfs2_xattr_def_value_root def_xv = {
98 98
99struct xattr_handler *ocfs2_xattr_handlers[] = { 99struct xattr_handler *ocfs2_xattr_handlers[] = {
100 &ocfs2_xattr_user_handler, 100 &ocfs2_xattr_user_handler,
101#ifdef CONFIG_OCFS2_FS_POSIX_ACL
102 &ocfs2_xattr_acl_access_handler, 101 &ocfs2_xattr_acl_access_handler,
103 &ocfs2_xattr_acl_default_handler, 102 &ocfs2_xattr_acl_default_handler,
104#endif
105 &ocfs2_xattr_trusted_handler, 103 &ocfs2_xattr_trusted_handler,
106 &ocfs2_xattr_security_handler, 104 &ocfs2_xattr_security_handler,
107 NULL 105 NULL
@@ -109,21 +107,20 @@ struct xattr_handler *ocfs2_xattr_handlers[] = {
109 107
110static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = { 108static struct xattr_handler *ocfs2_xattr_handler_map[OCFS2_XATTR_MAX] = {
111 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler, 109 [OCFS2_XATTR_INDEX_USER] = &ocfs2_xattr_user_handler,
112#ifdef CONFIG_OCFS2_FS_POSIX_ACL
113 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS] 110 [OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS]
114 = &ocfs2_xattr_acl_access_handler, 111 = &ocfs2_xattr_acl_access_handler,
115 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT] 112 [OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT]
116 = &ocfs2_xattr_acl_default_handler, 113 = &ocfs2_xattr_acl_default_handler,
117#endif
118 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler, 114 [OCFS2_XATTR_INDEX_TRUSTED] = &ocfs2_xattr_trusted_handler,
119 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler, 115 [OCFS2_XATTR_INDEX_SECURITY] = &ocfs2_xattr_security_handler,
120}; 116};
121 117
122struct ocfs2_xattr_info { 118struct ocfs2_xattr_info {
123 int name_index; 119 int xi_name_index;
124 const char *name; 120 const char *xi_name;
125 const void *value; 121 int xi_name_len;
126 size_t value_len; 122 const void *xi_value;
123 size_t xi_value_len;
127}; 124};
128 125
129struct ocfs2_xattr_search { 126struct ocfs2_xattr_search {
@@ -141,6 +138,115 @@ struct ocfs2_xattr_search {
141 int not_found; 138 int not_found;
142}; 139};
143 140
141/* Operations on struct ocfs2_xa_entry */
142struct ocfs2_xa_loc;
143struct ocfs2_xa_loc_operations {
144 /*
145 * Journal functions
146 */
147 int (*xlo_journal_access)(handle_t *handle, struct ocfs2_xa_loc *loc,
148 int type);
149 void (*xlo_journal_dirty)(handle_t *handle, struct ocfs2_xa_loc *loc);
150
151 /*
152 * Return a pointer to the appropriate buffer in loc->xl_storage
153 * at the given offset from loc->xl_header.
154 */
155 void *(*xlo_offset_pointer)(struct ocfs2_xa_loc *loc, int offset);
156
157 /* Can we reuse the existing entry for the new value? */
158 int (*xlo_can_reuse)(struct ocfs2_xa_loc *loc,
159 struct ocfs2_xattr_info *xi);
160
161 /* How much space is needed for the new value? */
162 int (*xlo_check_space)(struct ocfs2_xa_loc *loc,
163 struct ocfs2_xattr_info *xi);
164
165 /*
166 * Return the offset of the first name+value pair. This is
167 * the start of our downward-filling free space.
168 */
169 int (*xlo_get_free_start)(struct ocfs2_xa_loc *loc);
170
171 /*
172 * Remove the name+value at this location. Do whatever is
173 * appropriate with the remaining name+value pairs.
174 */
175 void (*xlo_wipe_namevalue)(struct ocfs2_xa_loc *loc);
176
177 /* Fill xl_entry with a new entry */
178 void (*xlo_add_entry)(struct ocfs2_xa_loc *loc, u32 name_hash);
179
180 /* Add name+value storage to an entry */
181 void (*xlo_add_namevalue)(struct ocfs2_xa_loc *loc, int size);
182
183 /*
184 * Initialize the value buf's access and bh fields for this entry.
185 * ocfs2_xa_fill_value_buf() will handle the xv pointer.
186 */
187 void (*xlo_fill_value_buf)(struct ocfs2_xa_loc *loc,
188 struct ocfs2_xattr_value_buf *vb);
189};
190
191/*
192 * Describes an xattr entry location. This is a memory structure
193 * tracking the on-disk structure.
194 */
195struct ocfs2_xa_loc {
196 /* This xattr belongs to this inode */
197 struct inode *xl_inode;
198
199 /* The ocfs2_xattr_header inside the on-disk storage. Not NULL. */
200 struct ocfs2_xattr_header *xl_header;
201
202 /* Bytes from xl_header to the end of the storage */
203 int xl_size;
204
205 /*
206 * The ocfs2_xattr_entry this location describes. If this is
207 * NULL, this location describes the on-disk structure where it
208 * would have been.
209 */
210 struct ocfs2_xattr_entry *xl_entry;
211
212 /*
213 * Internal housekeeping
214 */
215
216 /* Buffer(s) containing this entry */
217 void *xl_storage;
218
219 /* Operations on the storage backing this location */
220 const struct ocfs2_xa_loc_operations *xl_ops;
221};
222
223/*
224 * Convenience functions to calculate how much space is needed for a
225 * given name+value pair
226 */
227static int namevalue_size(int name_len, uint64_t value_len)
228{
229 if (value_len > OCFS2_XATTR_INLINE_SIZE)
230 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
231 else
232 return OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
233}
234
235static int namevalue_size_xi(struct ocfs2_xattr_info *xi)
236{
237 return namevalue_size(xi->xi_name_len, xi->xi_value_len);
238}
239
240static int namevalue_size_xe(struct ocfs2_xattr_entry *xe)
241{
242 u64 value_len = le64_to_cpu(xe->xe_value_size);
243
244 BUG_ON((value_len > OCFS2_XATTR_INLINE_SIZE) &&
245 ocfs2_xattr_is_local(xe));
246 return namevalue_size(xe->xe_name_len, value_len);
247}
248
249
144static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb, 250static int ocfs2_xattr_bucket_get_name_value(struct super_block *sb,
145 struct ocfs2_xattr_header *xh, 251 struct ocfs2_xattr_header *xh,
146 int index, 252 int index,
@@ -205,8 +311,6 @@ static int ocfs2_get_xattr_tree_value_root(struct super_block *sb,
205 int offset, 311 int offset,
206 struct ocfs2_xattr_value_root **xv, 312 struct ocfs2_xattr_value_root **xv,
207 struct buffer_head **bh); 313 struct buffer_head **bh);
208static int ocfs2_xattr_security_set(struct inode *inode, const char *name,
209 const void *value, size_t size, int flags);
210 314
211static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb) 315static inline u16 ocfs2_xattr_buckets_per_cluster(struct ocfs2_super *osb)
212{ 316{
@@ -218,14 +322,6 @@ static inline u16 ocfs2_blocks_per_xattr_bucket(struct super_block *sb)
218 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits); 322 return OCFS2_XATTR_BUCKET_SIZE / (1 << sb->s_blocksize_bits);
219} 323}
220 324
221static inline u16 ocfs2_xattr_max_xe_in_bucket(struct super_block *sb)
222{
223 u16 len = sb->s_blocksize -
224 offsetof(struct ocfs2_xattr_header, xh_entries);
225
226 return len / sizeof(struct ocfs2_xattr_entry);
227}
228
229#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr) 325#define bucket_blkno(_b) ((_b)->bu_bhs[0]->b_blocknr)
230#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data) 326#define bucket_block(_b, _n) ((_b)->bu_bhs[(_n)]->b_data)
231#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0)) 327#define bucket_xh(_b) ((struct ocfs2_xattr_header *)bucket_block((_b), 0))
@@ -469,35 +565,22 @@ static u32 ocfs2_xattr_name_hash(struct inode *inode,
469 return hash; 565 return hash;
470} 566}
471 567
472/* 568static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len)
473 * ocfs2_xattr_hash_entry()
474 *
475 * Compute the hash of an extended attribute.
476 */
477static void ocfs2_xattr_hash_entry(struct inode *inode,
478 struct ocfs2_xattr_header *header,
479 struct ocfs2_xattr_entry *entry)
480{ 569{
481 u32 hash = 0; 570 return namevalue_size(name_len, value_len) +
482 char *name = (char *)header + le16_to_cpu(entry->xe_name_offset); 571 sizeof(struct ocfs2_xattr_entry);
483
484 hash = ocfs2_xattr_name_hash(inode, name, entry->xe_name_len);
485 entry->xe_name_hash = cpu_to_le32(hash);
486
487 return;
488} 572}
489 573
490static int ocfs2_xattr_entry_real_size(int name_len, size_t value_len) 574static int ocfs2_xi_entry_usage(struct ocfs2_xattr_info *xi)
491{ 575{
492 int size = 0; 576 return namevalue_size_xi(xi) +
493 577 sizeof(struct ocfs2_xattr_entry);
494 if (value_len <= OCFS2_XATTR_INLINE_SIZE) 578}
495 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(value_len);
496 else
497 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
498 size += sizeof(struct ocfs2_xattr_entry);
499 579
500 return size; 580static int ocfs2_xe_entry_usage(struct ocfs2_xattr_entry *xe)
581{
582 return namevalue_size_xe(xe) +
583 sizeof(struct ocfs2_xattr_entry);
501} 584}
502 585
503int ocfs2_calc_security_init(struct inode *dir, 586int ocfs2_calc_security_init(struct inode *dir,
@@ -1314,452 +1397,897 @@ out:
1314 return ret; 1397 return ret;
1315} 1398}
1316 1399
1317static int ocfs2_xattr_cleanup(struct inode *inode, 1400static int ocfs2_xa_check_space_helper(int needed_space, int free_start,
1318 handle_t *handle, 1401 int num_entries)
1319 struct ocfs2_xattr_info *xi,
1320 struct ocfs2_xattr_search *xs,
1321 struct ocfs2_xattr_value_buf *vb,
1322 size_t offs)
1323{ 1402{
1324 int ret = 0; 1403 int free_space;
1325 size_t name_len = strlen(xi->name);
1326 void *val = xs->base + offs;
1327 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1328 1404
1329 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, 1405 if (!needed_space)
1330 OCFS2_JOURNAL_ACCESS_WRITE); 1406 return 0;
1331 if (ret) {
1332 mlog_errno(ret);
1333 goto out;
1334 }
1335 /* Decrease xattr count */
1336 le16_add_cpu(&xs->header->xh_count, -1);
1337 /* Remove the xattr entry and tree root which has already be set*/
1338 memset((void *)xs->here, 0, sizeof(struct ocfs2_xattr_entry));
1339 memset(val, 0, size);
1340 1407
1341 ret = ocfs2_journal_dirty(handle, vb->vb_bh); 1408 free_space = free_start -
1342 if (ret < 0) 1409 sizeof(struct ocfs2_xattr_header) -
1343 mlog_errno(ret); 1410 (num_entries * sizeof(struct ocfs2_xattr_entry)) -
1344out: 1411 OCFS2_XATTR_HEADER_GAP;
1345 return ret; 1412 if (free_space < 0)
1413 return -EIO;
1414 if (free_space < needed_space)
1415 return -ENOSPC;
1416
1417 return 0;
1346} 1418}
1347 1419
1348static int ocfs2_xattr_update_entry(struct inode *inode, 1420static int ocfs2_xa_journal_access(handle_t *handle, struct ocfs2_xa_loc *loc,
1349 handle_t *handle, 1421 int type)
1350 struct ocfs2_xattr_info *xi,
1351 struct ocfs2_xattr_search *xs,
1352 struct ocfs2_xattr_value_buf *vb,
1353 size_t offs)
1354{ 1422{
1355 int ret; 1423 return loc->xl_ops->xlo_journal_access(handle, loc, type);
1424}
1356 1425
1357 ret = vb->vb_access(handle, INODE_CACHE(inode), vb->vb_bh, 1426static void ocfs2_xa_journal_dirty(handle_t *handle, struct ocfs2_xa_loc *loc)
1358 OCFS2_JOURNAL_ACCESS_WRITE); 1427{
1359 if (ret) { 1428 loc->xl_ops->xlo_journal_dirty(handle, loc);
1360 mlog_errno(ret); 1429}
1361 goto out;
1362 }
1363 1430
1364 xs->here->xe_name_offset = cpu_to_le16(offs); 1431/* Give a pointer into the storage for the given offset */
1365 xs->here->xe_value_size = cpu_to_le64(xi->value_len); 1432static void *ocfs2_xa_offset_pointer(struct ocfs2_xa_loc *loc, int offset)
1366 if (xi->value_len <= OCFS2_XATTR_INLINE_SIZE) 1433{
1367 ocfs2_xattr_set_local(xs->here, 1); 1434 BUG_ON(offset >= loc->xl_size);
1368 else 1435 return loc->xl_ops->xlo_offset_pointer(loc, offset);
1369 ocfs2_xattr_set_local(xs->here, 0); 1436}
1370 ocfs2_xattr_hash_entry(inode, xs->header, xs->here);
1371 1437
1372 ret = ocfs2_journal_dirty(handle, vb->vb_bh); 1438/*
1373 if (ret < 0) 1439 * Wipe the name+value pair and allow the storage to reclaim it. This
1374 mlog_errno(ret); 1440 * must be followed by either removal of the entry or a call to
1375out: 1441 * ocfs2_xa_add_namevalue().
1376 return ret; 1442 */
1443static void ocfs2_xa_wipe_namevalue(struct ocfs2_xa_loc *loc)
1444{
1445 loc->xl_ops->xlo_wipe_namevalue(loc);
1377} 1446}
1378 1447
1379/* 1448/*
1380 * ocfs2_xattr_set_value_outside() 1449 * Find lowest offset to a name+value pair. This is the start of our
1381 * 1450 * downward-growing free space.
1382 * Set large size value in B tree.
1383 */ 1451 */
1384static int ocfs2_xattr_set_value_outside(struct inode *inode, 1452static int ocfs2_xa_get_free_start(struct ocfs2_xa_loc *loc)
1385 struct ocfs2_xattr_info *xi,
1386 struct ocfs2_xattr_search *xs,
1387 struct ocfs2_xattr_set_ctxt *ctxt,
1388 struct ocfs2_xattr_value_buf *vb,
1389 size_t offs)
1390{ 1453{
1391 size_t name_len = strlen(xi->name); 1454 return loc->xl_ops->xlo_get_free_start(loc);
1392 void *val = xs->base + offs; 1455}
1393 struct ocfs2_xattr_value_root *xv = NULL;
1394 size_t size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE;
1395 int ret = 0;
1396 1456
1397 memset(val, 0, size); 1457/* Can we reuse loc->xl_entry for xi? */
1398 memcpy(val, xi->name, name_len); 1458static int ocfs2_xa_can_reuse_entry(struct ocfs2_xa_loc *loc,
1399 xv = (struct ocfs2_xattr_value_root *) 1459 struct ocfs2_xattr_info *xi)
1400 (val + OCFS2_XATTR_SIZE(name_len)); 1460{
1401 xv->xr_clusters = 0; 1461 return loc->xl_ops->xlo_can_reuse(loc, xi);
1402 xv->xr_last_eb_blk = 0; 1462}
1403 xv->xr_list.l_tree_depth = 0; 1463
1404 xv->xr_list.l_count = cpu_to_le16(1); 1464/* How much free space is needed to set the new value */
1405 xv->xr_list.l_next_free_rec = 0; 1465static int ocfs2_xa_check_space(struct ocfs2_xa_loc *loc,
1406 vb->vb_xv = xv; 1466 struct ocfs2_xattr_info *xi)
1407 1467{
1408 ret = ocfs2_xattr_value_truncate(inode, vb, xi->value_len, ctxt); 1468 return loc->xl_ops->xlo_check_space(loc, xi);
1409 if (ret < 0) { 1469}
1410 mlog_errno(ret); 1470
1411 return ret; 1471static void ocfs2_xa_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1472{
1473 loc->xl_ops->xlo_add_entry(loc, name_hash);
1474 loc->xl_entry->xe_name_hash = cpu_to_le32(name_hash);
1475 /*
1476 * We can't leave the new entry's xe_name_offset at zero or
1477 * add_namevalue() will go nuts. We set it to the size of our
1478 * storage so that it can never be less than any other entry.
1479 */
1480 loc->xl_entry->xe_name_offset = cpu_to_le16(loc->xl_size);
1481}
1482
1483static void ocfs2_xa_add_namevalue(struct ocfs2_xa_loc *loc,
1484 struct ocfs2_xattr_info *xi)
1485{
1486 int size = namevalue_size_xi(xi);
1487 int nameval_offset;
1488 char *nameval_buf;
1489
1490 loc->xl_ops->xlo_add_namevalue(loc, size);
1491 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1492 loc->xl_entry->xe_name_len = xi->xi_name_len;
1493 ocfs2_xattr_set_type(loc->xl_entry, xi->xi_name_index);
1494 ocfs2_xattr_set_local(loc->xl_entry,
1495 xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE);
1496
1497 nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1498 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
1499 memset(nameval_buf, 0, size);
1500 memcpy(nameval_buf, xi->xi_name, xi->xi_name_len);
1501}
1502
1503static void ocfs2_xa_fill_value_buf(struct ocfs2_xa_loc *loc,
1504 struct ocfs2_xattr_value_buf *vb)
1505{
1506 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1507 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1508
1509 /* Value bufs are for value trees */
1510 BUG_ON(ocfs2_xattr_is_local(loc->xl_entry));
1511 BUG_ON(namevalue_size_xe(loc->xl_entry) !=
1512 (name_size + OCFS2_XATTR_ROOT_SIZE));
1513
1514 loc->xl_ops->xlo_fill_value_buf(loc, vb);
1515 vb->vb_xv =
1516 (struct ocfs2_xattr_value_root *)ocfs2_xa_offset_pointer(loc,
1517 nameval_offset +
1518 name_size);
1519}
1520
1521static int ocfs2_xa_block_journal_access(handle_t *handle,
1522 struct ocfs2_xa_loc *loc, int type)
1523{
1524 struct buffer_head *bh = loc->xl_storage;
1525 ocfs2_journal_access_func access;
1526
1527 if (loc->xl_size == (bh->b_size -
1528 offsetof(struct ocfs2_xattr_block,
1529 xb_attrs.xb_header)))
1530 access = ocfs2_journal_access_xb;
1531 else
1532 access = ocfs2_journal_access_di;
1533 return access(handle, INODE_CACHE(loc->xl_inode), bh, type);
1534}
1535
1536static void ocfs2_xa_block_journal_dirty(handle_t *handle,
1537 struct ocfs2_xa_loc *loc)
1538{
1539 struct buffer_head *bh = loc->xl_storage;
1540
1541 ocfs2_journal_dirty(handle, bh);
1542}
1543
1544static void *ocfs2_xa_block_offset_pointer(struct ocfs2_xa_loc *loc,
1545 int offset)
1546{
1547 return (char *)loc->xl_header + offset;
1548}
1549
1550static int ocfs2_xa_block_can_reuse(struct ocfs2_xa_loc *loc,
1551 struct ocfs2_xattr_info *xi)
1552{
1553 /*
1554 * Block storage is strict. If the sizes aren't exact, we will
1555 * remove the old one and reinsert the new.
1556 */
1557 return namevalue_size_xe(loc->xl_entry) ==
1558 namevalue_size_xi(xi);
1559}
1560
1561static int ocfs2_xa_block_get_free_start(struct ocfs2_xa_loc *loc)
1562{
1563 struct ocfs2_xattr_header *xh = loc->xl_header;
1564 int i, count = le16_to_cpu(xh->xh_count);
1565 int offset, free_start = loc->xl_size;
1566
1567 for (i = 0; i < count; i++) {
1568 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1569 if (offset < free_start)
1570 free_start = offset;
1412 } 1571 }
1413 ret = ocfs2_xattr_update_entry(inode, ctxt->handle, xi, xs, vb, offs); 1572
1414 if (ret < 0) { 1573 return free_start;
1415 mlog_errno(ret); 1574}
1416 return ret; 1575
1576static int ocfs2_xa_block_check_space(struct ocfs2_xa_loc *loc,
1577 struct ocfs2_xattr_info *xi)
1578{
1579 int count = le16_to_cpu(loc->xl_header->xh_count);
1580 int free_start = ocfs2_xa_get_free_start(loc);
1581 int needed_space = ocfs2_xi_entry_usage(xi);
1582
1583 /*
1584 * Block storage will reclaim the original entry before inserting
1585 * the new value, so we only need the difference. If the new
1586 * entry is smaller than the old one, we don't need anything.
1587 */
1588 if (loc->xl_entry) {
1589 /* Don't need space if we're reusing! */
1590 if (ocfs2_xa_can_reuse_entry(loc, xi))
1591 needed_space = 0;
1592 else
1593 needed_space -= ocfs2_xe_entry_usage(loc->xl_entry);
1417 } 1594 }
1418 ret = __ocfs2_xattr_set_value_outside(inode, ctxt->handle, vb, 1595 if (needed_space < 0)
1419 xi->value, xi->value_len); 1596 needed_space = 0;
1420 if (ret < 0) 1597 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1421 mlog_errno(ret); 1598}
1422 1599
1423 return ret; 1600/*
1601 * Block storage for xattrs keeps the name+value pairs compacted. When
1602 * we remove one, we have to shift any that preceded it towards the end.
1603 */
1604static void ocfs2_xa_block_wipe_namevalue(struct ocfs2_xa_loc *loc)
1605{
1606 int i, offset;
1607 int namevalue_offset, first_namevalue_offset, namevalue_size;
1608 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1609 struct ocfs2_xattr_header *xh = loc->xl_header;
1610 int count = le16_to_cpu(xh->xh_count);
1611
1612 namevalue_offset = le16_to_cpu(entry->xe_name_offset);
1613 namevalue_size = namevalue_size_xe(entry);
1614 first_namevalue_offset = ocfs2_xa_get_free_start(loc);
1615
1616 /* Shift the name+value pairs */
1617 memmove((char *)xh + first_namevalue_offset + namevalue_size,
1618 (char *)xh + first_namevalue_offset,
1619 namevalue_offset - first_namevalue_offset);
1620 memset((char *)xh + first_namevalue_offset, 0, namevalue_size);
1621
1622 /* Now tell xh->xh_entries about it */
1623 for (i = 0; i < count; i++) {
1624 offset = le16_to_cpu(xh->xh_entries[i].xe_name_offset);
1625 if (offset <= namevalue_offset)
1626 le16_add_cpu(&xh->xh_entries[i].xe_name_offset,
1627 namevalue_size);
1628 }
1629
1630 /*
1631 * Note that we don't update xh_free_start or xh_name_value_len
1632 * because they're not used in block-stored xattrs.
1633 */
1634}
1635
1636static void ocfs2_xa_block_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1637{
1638 int count = le16_to_cpu(loc->xl_header->xh_count);
1639 loc->xl_entry = &(loc->xl_header->xh_entries[count]);
1640 le16_add_cpu(&loc->xl_header->xh_count, 1);
1641 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1642}
1643
1644static void ocfs2_xa_block_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1645{
1646 int free_start = ocfs2_xa_get_free_start(loc);
1647
1648 loc->xl_entry->xe_name_offset = cpu_to_le16(free_start - size);
1649}
1650
1651static void ocfs2_xa_block_fill_value_buf(struct ocfs2_xa_loc *loc,
1652 struct ocfs2_xattr_value_buf *vb)
1653{
1654 struct buffer_head *bh = loc->xl_storage;
1655
1656 if (loc->xl_size == (bh->b_size -
1657 offsetof(struct ocfs2_xattr_block,
1658 xb_attrs.xb_header)))
1659 vb->vb_access = ocfs2_journal_access_xb;
1660 else
1661 vb->vb_access = ocfs2_journal_access_di;
1662 vb->vb_bh = bh;
1424} 1663}
1425 1664
1426/* 1665/*
1427 * ocfs2_xattr_set_entry_local() 1666 * Operations for xattrs stored in blocks. This includes inline inode
1428 * 1667 * storage and unindexed ocfs2_xattr_blocks.
1429 * Set, replace or remove extended attribute in local.
1430 */ 1668 */
1431static void ocfs2_xattr_set_entry_local(struct inode *inode, 1669static const struct ocfs2_xa_loc_operations ocfs2_xa_block_loc_ops = {
1432 struct ocfs2_xattr_info *xi, 1670 .xlo_journal_access = ocfs2_xa_block_journal_access,
1433 struct ocfs2_xattr_search *xs, 1671 .xlo_journal_dirty = ocfs2_xa_block_journal_dirty,
1434 struct ocfs2_xattr_entry *last, 1672 .xlo_offset_pointer = ocfs2_xa_block_offset_pointer,
1435 size_t min_offs) 1673 .xlo_check_space = ocfs2_xa_block_check_space,
1674 .xlo_can_reuse = ocfs2_xa_block_can_reuse,
1675 .xlo_get_free_start = ocfs2_xa_block_get_free_start,
1676 .xlo_wipe_namevalue = ocfs2_xa_block_wipe_namevalue,
1677 .xlo_add_entry = ocfs2_xa_block_add_entry,
1678 .xlo_add_namevalue = ocfs2_xa_block_add_namevalue,
1679 .xlo_fill_value_buf = ocfs2_xa_block_fill_value_buf,
1680};
1681
1682static int ocfs2_xa_bucket_journal_access(handle_t *handle,
1683 struct ocfs2_xa_loc *loc, int type)
1436{ 1684{
1437 size_t name_len = strlen(xi->name); 1685 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1438 int i;
1439 1686
1440 if (xi->value && xs->not_found) { 1687 return ocfs2_xattr_bucket_journal_access(handle, bucket, type);
1441 /* Insert the new xattr entry. */ 1688}
1442 le16_add_cpu(&xs->header->xh_count, 1); 1689
1443 ocfs2_xattr_set_type(last, xi->name_index); 1690static void ocfs2_xa_bucket_journal_dirty(handle_t *handle,
1444 ocfs2_xattr_set_local(last, 1); 1691 struct ocfs2_xa_loc *loc)
1445 last->xe_name_len = name_len; 1692{
1446 } else { 1693 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1447 void *first_val; 1694
1448 void *val; 1695 ocfs2_xattr_bucket_journal_dirty(handle, bucket);
1449 size_t offs, size; 1696}
1450 1697
1451 first_val = xs->base + min_offs; 1698static void *ocfs2_xa_bucket_offset_pointer(struct ocfs2_xa_loc *loc,
1452 offs = le16_to_cpu(xs->here->xe_name_offset); 1699 int offset)
1453 val = xs->base + offs; 1700{
1454 1701 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1455 if (le64_to_cpu(xs->here->xe_value_size) > 1702 int block, block_offset;
1456 OCFS2_XATTR_INLINE_SIZE) 1703
1457 size = OCFS2_XATTR_SIZE(name_len) + 1704 /* The header is at the front of the bucket */
1458 OCFS2_XATTR_ROOT_SIZE; 1705 block = offset >> loc->xl_inode->i_sb->s_blocksize_bits;
1706 block_offset = offset % loc->xl_inode->i_sb->s_blocksize;
1707
1708 return bucket_block(bucket, block) + block_offset;
1709}
1710
1711static int ocfs2_xa_bucket_can_reuse(struct ocfs2_xa_loc *loc,
1712 struct ocfs2_xattr_info *xi)
1713{
1714 return namevalue_size_xe(loc->xl_entry) >=
1715 namevalue_size_xi(xi);
1716}
1717
1718static int ocfs2_xa_bucket_get_free_start(struct ocfs2_xa_loc *loc)
1719{
1720 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1721 return le16_to_cpu(bucket_xh(bucket)->xh_free_start);
1722}
1723
1724static int ocfs2_bucket_align_free_start(struct super_block *sb,
1725 int free_start, int size)
1726{
1727 /*
1728 * We need to make sure that the name+value pair fits within
1729 * one block.
1730 */
1731 if (((free_start - size) >> sb->s_blocksize_bits) !=
1732 ((free_start - 1) >> sb->s_blocksize_bits))
1733 free_start -= free_start % sb->s_blocksize;
1734
1735 return free_start;
1736}
1737
1738static int ocfs2_xa_bucket_check_space(struct ocfs2_xa_loc *loc,
1739 struct ocfs2_xattr_info *xi)
1740{
1741 int rc;
1742 int count = le16_to_cpu(loc->xl_header->xh_count);
1743 int free_start = ocfs2_xa_get_free_start(loc);
1744 int needed_space = ocfs2_xi_entry_usage(xi);
1745 int size = namevalue_size_xi(xi);
1746 struct super_block *sb = loc->xl_inode->i_sb;
1747
1748 /*
1749 * Bucket storage does not reclaim name+value pairs it cannot
1750 * reuse. They live as holes until the bucket fills, and then
1751 * the bucket is defragmented. However, the bucket can reclaim
1752 * the ocfs2_xattr_entry.
1753 */
1754 if (loc->xl_entry) {
1755 /* Don't need space if we're reusing! */
1756 if (ocfs2_xa_can_reuse_entry(loc, xi))
1757 needed_space = 0;
1459 else 1758 else
1460 size = OCFS2_XATTR_SIZE(name_len) + 1759 needed_space -= sizeof(struct ocfs2_xattr_entry);
1461 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); 1760 }
1462 1761 BUG_ON(needed_space < 0);
1463 if (xi->value && size == OCFS2_XATTR_SIZE(name_len) +
1464 OCFS2_XATTR_SIZE(xi->value_len)) {
1465 /* The old and the new value have the
1466 same size. Just replace the value. */
1467 ocfs2_xattr_set_local(xs->here, 1);
1468 xs->here->xe_value_size = cpu_to_le64(xi->value_len);
1469 /* Clear value bytes. */
1470 memset(val + OCFS2_XATTR_SIZE(name_len),
1471 0,
1472 OCFS2_XATTR_SIZE(xi->value_len));
1473 memcpy(val + OCFS2_XATTR_SIZE(name_len),
1474 xi->value,
1475 xi->value_len);
1476 return;
1477 }
1478 /* Remove the old name+value. */
1479 memmove(first_val + size, first_val, val - first_val);
1480 memset(first_val, 0, size);
1481 xs->here->xe_name_hash = 0;
1482 xs->here->xe_name_offset = 0;
1483 ocfs2_xattr_set_local(xs->here, 1);
1484 xs->here->xe_value_size = 0;
1485
1486 min_offs += size;
1487
1488 /* Adjust all value offsets. */
1489 last = xs->header->xh_entries;
1490 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) {
1491 size_t o = le16_to_cpu(last->xe_name_offset);
1492
1493 if (o < offs)
1494 last->xe_name_offset = cpu_to_le16(o + size);
1495 last += 1;
1496 }
1497 1762
1498 if (!xi->value) { 1763 if (free_start < size) {
1499 /* Remove the old entry. */ 1764 if (needed_space)
1500 last -= 1; 1765 return -ENOSPC;
1501 memmove(xs->here, xs->here + 1, 1766 } else {
1502 (void *)last - (void *)xs->here); 1767 /*
1503 memset(last, 0, sizeof(struct ocfs2_xattr_entry)); 1768 * First we check if it would fit in the first place.
1504 le16_add_cpu(&xs->header->xh_count, -1); 1769 * Below, we align the free start to a block. This may
1505 } 1770 * slide us below the minimum gap. By checking unaligned
1771 * first, we avoid that error.
1772 */
1773 rc = ocfs2_xa_check_space_helper(needed_space, free_start,
1774 count);
1775 if (rc)
1776 return rc;
1777 free_start = ocfs2_bucket_align_free_start(sb, free_start,
1778 size);
1506 } 1779 }
1507 if (xi->value) { 1780 return ocfs2_xa_check_space_helper(needed_space, free_start, count);
1508 /* Insert the new name+value. */ 1781}
1509 size_t size = OCFS2_XATTR_SIZE(name_len) + 1782
1510 OCFS2_XATTR_SIZE(xi->value_len); 1783static void ocfs2_xa_bucket_wipe_namevalue(struct ocfs2_xa_loc *loc)
1511 void *val = xs->base + min_offs - size; 1784{
1785 le16_add_cpu(&loc->xl_header->xh_name_value_len,
1786 -namevalue_size_xe(loc->xl_entry));
1787}
1788
1789static void ocfs2_xa_bucket_add_entry(struct ocfs2_xa_loc *loc, u32 name_hash)
1790{
1791 struct ocfs2_xattr_header *xh = loc->xl_header;
1792 int count = le16_to_cpu(xh->xh_count);
1793 int low = 0, high = count - 1, tmp;
1794 struct ocfs2_xattr_entry *tmp_xe;
1512 1795
1513 xs->here->xe_name_offset = cpu_to_le16(min_offs - size); 1796 /*
1514 memset(val, 0, size); 1797 * We keep buckets sorted by name_hash, so we need to find
1515 memcpy(val, xi->name, name_len); 1798 * our insert place.
1516 memcpy(val + OCFS2_XATTR_SIZE(name_len), 1799 */
1517 xi->value, 1800 while (low <= high && count) {
1518 xi->value_len); 1801 tmp = (low + high) / 2;
1519 xs->here->xe_value_size = cpu_to_le64(xi->value_len); 1802 tmp_xe = &xh->xh_entries[tmp];
1520 ocfs2_xattr_set_local(xs->here, 1); 1803
1521 ocfs2_xattr_hash_entry(inode, xs->header, xs->here); 1804 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
1805 low = tmp + 1;
1806 else if (name_hash < le32_to_cpu(tmp_xe->xe_name_hash))
1807 high = tmp - 1;
1808 else {
1809 low = tmp;
1810 break;
1811 }
1522 } 1812 }
1523 1813
1524 return; 1814 if (low != count)
1815 memmove(&xh->xh_entries[low + 1],
1816 &xh->xh_entries[low],
1817 ((count - low) * sizeof(struct ocfs2_xattr_entry)));
1818
1819 le16_add_cpu(&xh->xh_count, 1);
1820 loc->xl_entry = &xh->xh_entries[low];
1821 memset(loc->xl_entry, 0, sizeof(struct ocfs2_xattr_entry));
1822}
1823
1824static void ocfs2_xa_bucket_add_namevalue(struct ocfs2_xa_loc *loc, int size)
1825{
1826 int free_start = ocfs2_xa_get_free_start(loc);
1827 struct ocfs2_xattr_header *xh = loc->xl_header;
1828 struct super_block *sb = loc->xl_inode->i_sb;
1829 int nameval_offset;
1830
1831 free_start = ocfs2_bucket_align_free_start(sb, free_start, size);
1832 nameval_offset = free_start - size;
1833 loc->xl_entry->xe_name_offset = cpu_to_le16(nameval_offset);
1834 xh->xh_free_start = cpu_to_le16(nameval_offset);
1835 le16_add_cpu(&xh->xh_name_value_len, size);
1836
1837}
1838
1839static void ocfs2_xa_bucket_fill_value_buf(struct ocfs2_xa_loc *loc,
1840 struct ocfs2_xattr_value_buf *vb)
1841{
1842 struct ocfs2_xattr_bucket *bucket = loc->xl_storage;
1843 struct super_block *sb = loc->xl_inode->i_sb;
1844 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
1845 int size = namevalue_size_xe(loc->xl_entry);
1846 int block_offset = nameval_offset >> sb->s_blocksize_bits;
1847
1848 /* Values are not allowed to straddle block boundaries */
1849 BUG_ON(block_offset !=
1850 ((nameval_offset + size - 1) >> sb->s_blocksize_bits));
1851 /* We expect the bucket to be filled in */
1852 BUG_ON(!bucket->bu_bhs[block_offset]);
1853
1854 vb->vb_access = ocfs2_journal_access;
1855 vb->vb_bh = bucket->bu_bhs[block_offset];
1856}
1857
1858/* Operations for xattrs stored in buckets. */
1859static const struct ocfs2_xa_loc_operations ocfs2_xa_bucket_loc_ops = {
1860 .xlo_journal_access = ocfs2_xa_bucket_journal_access,
1861 .xlo_journal_dirty = ocfs2_xa_bucket_journal_dirty,
1862 .xlo_offset_pointer = ocfs2_xa_bucket_offset_pointer,
1863 .xlo_check_space = ocfs2_xa_bucket_check_space,
1864 .xlo_can_reuse = ocfs2_xa_bucket_can_reuse,
1865 .xlo_get_free_start = ocfs2_xa_bucket_get_free_start,
1866 .xlo_wipe_namevalue = ocfs2_xa_bucket_wipe_namevalue,
1867 .xlo_add_entry = ocfs2_xa_bucket_add_entry,
1868 .xlo_add_namevalue = ocfs2_xa_bucket_add_namevalue,
1869 .xlo_fill_value_buf = ocfs2_xa_bucket_fill_value_buf,
1870};
1871
1872static unsigned int ocfs2_xa_value_clusters(struct ocfs2_xa_loc *loc)
1873{
1874 struct ocfs2_xattr_value_buf vb;
1875
1876 if (ocfs2_xattr_is_local(loc->xl_entry))
1877 return 0;
1878
1879 ocfs2_xa_fill_value_buf(loc, &vb);
1880 return le32_to_cpu(vb.vb_xv->xr_clusters);
1881}
1882
1883static int ocfs2_xa_value_truncate(struct ocfs2_xa_loc *loc, u64 bytes,
1884 struct ocfs2_xattr_set_ctxt *ctxt)
1885{
1886 int trunc_rc, access_rc;
1887 struct ocfs2_xattr_value_buf vb;
1888
1889 ocfs2_xa_fill_value_buf(loc, &vb);
1890 trunc_rc = ocfs2_xattr_value_truncate(loc->xl_inode, &vb, bytes,
1891 ctxt);
1892
1893 /*
1894 * The caller of ocfs2_xa_value_truncate() has already called
1895 * ocfs2_xa_journal_access on the loc. However, The truncate code
1896 * calls ocfs2_extend_trans(). This may commit the previous
1897 * transaction and open a new one. If this is a bucket, truncate
1898 * could leave only vb->vb_bh set up for journaling. Meanwhile,
1899 * the caller is expecting to dirty the entire bucket. So we must
1900 * reset the journal work. We do this even if truncate has failed,
1901 * as it could have failed after committing the extend.
1902 */
1903 access_rc = ocfs2_xa_journal_access(ctxt->handle, loc,
1904 OCFS2_JOURNAL_ACCESS_WRITE);
1905
1906 /* Errors in truncate take precedence */
1907 return trunc_rc ? trunc_rc : access_rc;
1908}
1909
1910static void ocfs2_xa_remove_entry(struct ocfs2_xa_loc *loc)
1911{
1912 int index, count;
1913 struct ocfs2_xattr_header *xh = loc->xl_header;
1914 struct ocfs2_xattr_entry *entry = loc->xl_entry;
1915
1916 ocfs2_xa_wipe_namevalue(loc);
1917 loc->xl_entry = NULL;
1918
1919 le16_add_cpu(&xh->xh_count, -1);
1920 count = le16_to_cpu(xh->xh_count);
1921
1922 /*
1923 * Only zero out the entry if there are more remaining. This is
1924 * important for an empty bucket, as it keeps track of the
1925 * bucket's hash value. It doesn't hurt empty block storage.
1926 */
1927 if (count) {
1928 index = ((char *)entry - (char *)&xh->xh_entries) /
1929 sizeof(struct ocfs2_xattr_entry);
1930 memmove(&xh->xh_entries[index], &xh->xh_entries[index + 1],
1931 (count - index) * sizeof(struct ocfs2_xattr_entry));
1932 memset(&xh->xh_entries[count], 0,
1933 sizeof(struct ocfs2_xattr_entry));
1934 }
1525} 1935}
1526 1936
1527/* 1937/*
1528 * ocfs2_xattr_set_entry() 1938 * If we have a problem adjusting the size of an external value during
1939 * ocfs2_xa_prepare_entry() or ocfs2_xa_remove(), we may have an xattr
1940 * in an intermediate state. For example, the value may be partially
1941 * truncated.
1529 * 1942 *
1530 * Set extended attribute entry into inode or block. 1943 * If the value tree hasn't changed, the extend/truncate went nowhere.
1944 * We have nothing to do. The caller can treat it as a straight error.
1531 * 1945 *
1532 * If extended attribute value size > OCFS2_XATTR_INLINE_SIZE, 1946 * If the value tree got partially truncated, we now have a corrupted
1533 * We first insert tree root(ocfs2_xattr_value_root) with set_entry_local(), 1947 * extended attribute. We're going to wipe its entry and leak the
1534 * then set value in B tree with set_value_outside(). 1948 * clusters. Better to leak some storage than leave a corrupt entry.
1949 *
1950 * If the value tree grew, it obviously didn't grow enough for the
1951 * new entry. We're not going to try and reclaim those clusters either.
1952 * If there was already an external value there (orig_clusters != 0),
1953 * the new clusters are attached safely and we can just leave the old
1954 * value in place. If there was no external value there, we remove
1955 * the entry.
1956 *
1957 * This way, the xattr block we store in the journal will be consistent.
1958 * If the size change broke because of the journal, no changes will hit
1959 * disk anyway.
1535 */ 1960 */
1536static int ocfs2_xattr_set_entry(struct inode *inode, 1961static void ocfs2_xa_cleanup_value_truncate(struct ocfs2_xa_loc *loc,
1537 struct ocfs2_xattr_info *xi, 1962 const char *what,
1538 struct ocfs2_xattr_search *xs, 1963 unsigned int orig_clusters)
1539 struct ocfs2_xattr_set_ctxt *ctxt, 1964{
1540 int flag) 1965 unsigned int new_clusters = ocfs2_xa_value_clusters(loc);
1541{ 1966 char *nameval_buf = ocfs2_xa_offset_pointer(loc,
1542 struct ocfs2_xattr_entry *last; 1967 le16_to_cpu(loc->xl_entry->xe_name_offset));
1543 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1968
1544 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; 1969 if (new_clusters < orig_clusters) {
1545 size_t min_offs = xs->end - xs->base, name_len = strlen(xi->name); 1970 mlog(ML_ERROR,
1546 size_t size_l = 0; 1971 "Partial truncate while %s xattr %.*s. Leaking "
1547 handle_t *handle = ctxt->handle; 1972 "%u clusters and removing the entry\n",
1548 int free, i, ret; 1973 what, loc->xl_entry->xe_name_len, nameval_buf,
1549 struct ocfs2_xattr_info xi_l = { 1974 orig_clusters - new_clusters);
1550 .name_index = xi->name_index, 1975 ocfs2_xa_remove_entry(loc);
1551 .name = xi->name, 1976 } else if (!orig_clusters) {
1552 .value = xi->value, 1977 mlog(ML_ERROR,
1553 .value_len = xi->value_len, 1978 "Unable to allocate an external value for xattr "
1554 }; 1979 "%.*s safely. Leaking %u clusters and removing the "
1555 struct ocfs2_xattr_value_buf vb = { 1980 "entry\n",
1556 .vb_bh = xs->xattr_bh, 1981 loc->xl_entry->xe_name_len, nameval_buf,
1557 .vb_access = ocfs2_journal_access_di, 1982 new_clusters - orig_clusters);
1558 }; 1983 ocfs2_xa_remove_entry(loc);
1984 } else if (new_clusters > orig_clusters)
1985 mlog(ML_ERROR,
1986 "Unable to grow xattr %.*s safely. %u new clusters "
1987 "have been added, but the value will not be "
1988 "modified\n",
1989 loc->xl_entry->xe_name_len, nameval_buf,
1990 new_clusters - orig_clusters);
1991}
1992
1993static int ocfs2_xa_remove(struct ocfs2_xa_loc *loc,
1994 struct ocfs2_xattr_set_ctxt *ctxt)
1995{
1996 int rc = 0;
1997 unsigned int orig_clusters;
1998
1999 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2000 orig_clusters = ocfs2_xa_value_clusters(loc);
2001 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2002 if (rc) {
2003 mlog_errno(rc);
2004 /*
2005 * Since this is remove, we can return 0 if
2006 * ocfs2_xa_cleanup_value_truncate() is going to
2007 * wipe the entry anyway. So we check the
2008 * cluster count as well.
2009 */
2010 if (orig_clusters != ocfs2_xa_value_clusters(loc))
2011 rc = 0;
2012 ocfs2_xa_cleanup_value_truncate(loc, "removing",
2013 orig_clusters);
2014 if (rc)
2015 goto out;
2016 }
2017 }
1559 2018
1560 if (!(flag & OCFS2_INLINE_XATTR_FL)) { 2019 ocfs2_xa_remove_entry(loc);
1561 BUG_ON(xs->xattr_bh == xs->inode_bh);
1562 vb.vb_access = ocfs2_journal_access_xb;
1563 } else
1564 BUG_ON(xs->xattr_bh != xs->inode_bh);
1565 2020
1566 /* Compute min_offs, last and free space. */ 2021out:
1567 last = xs->header->xh_entries; 2022 return rc;
2023}
1568 2024
1569 for (i = 0 ; i < le16_to_cpu(xs->header->xh_count); i++) { 2025static void ocfs2_xa_install_value_root(struct ocfs2_xa_loc *loc)
1570 size_t offs = le16_to_cpu(last->xe_name_offset); 2026{
1571 if (offs < min_offs) 2027 int name_size = OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len);
1572 min_offs = offs; 2028 char *nameval_buf;
1573 last += 1;
1574 }
1575 2029
1576 free = min_offs - ((void *)last - xs->base) - OCFS2_XATTR_HEADER_GAP; 2030 nameval_buf = ocfs2_xa_offset_pointer(loc,
1577 if (free < 0) 2031 le16_to_cpu(loc->xl_entry->xe_name_offset));
1578 return -EIO; 2032 memcpy(nameval_buf + name_size, &def_xv, OCFS2_XATTR_ROOT_SIZE);
2033}
1579 2034
1580 if (!xs->not_found) { 2035/*
1581 size_t size = 0; 2036 * Take an existing entry and make it ready for the new value. This
1582 if (ocfs2_xattr_is_local(xs->here)) 2037 * won't allocate space, but it may free space. It should be ready for
1583 size = OCFS2_XATTR_SIZE(name_len) + 2038 * ocfs2_xa_prepare_entry() to finish the work.
1584 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size)); 2039 */
1585 else 2040static int ocfs2_xa_reuse_entry(struct ocfs2_xa_loc *loc,
1586 size = OCFS2_XATTR_SIZE(name_len) + 2041 struct ocfs2_xattr_info *xi,
1587 OCFS2_XATTR_ROOT_SIZE; 2042 struct ocfs2_xattr_set_ctxt *ctxt)
1588 free += (size + sizeof(struct ocfs2_xattr_entry)); 2043{
1589 } 2044 int rc = 0;
1590 /* Check free space in inode or block */ 2045 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
1591 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { 2046 unsigned int orig_clusters;
1592 if (free < sizeof(struct ocfs2_xattr_entry) + 2047 char *nameval_buf;
1593 OCFS2_XATTR_SIZE(name_len) + 2048 int xe_local = ocfs2_xattr_is_local(loc->xl_entry);
1594 OCFS2_XATTR_ROOT_SIZE) { 2049 int xi_local = xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE;
1595 ret = -ENOSPC; 2050
1596 goto out; 2051 BUG_ON(OCFS2_XATTR_SIZE(loc->xl_entry->xe_name_len) !=
2052 name_size);
2053
2054 nameval_buf = ocfs2_xa_offset_pointer(loc,
2055 le16_to_cpu(loc->xl_entry->xe_name_offset));
2056 if (xe_local) {
2057 memset(nameval_buf + name_size, 0,
2058 namevalue_size_xe(loc->xl_entry) - name_size);
2059 if (!xi_local)
2060 ocfs2_xa_install_value_root(loc);
2061 } else {
2062 orig_clusters = ocfs2_xa_value_clusters(loc);
2063 if (xi_local) {
2064 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2065 if (rc < 0)
2066 mlog_errno(rc);
2067 else
2068 memset(nameval_buf + name_size, 0,
2069 namevalue_size_xe(loc->xl_entry) -
2070 name_size);
2071 } else if (le64_to_cpu(loc->xl_entry->xe_value_size) >
2072 xi->xi_value_len) {
2073 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len,
2074 ctxt);
2075 if (rc < 0)
2076 mlog_errno(rc);
1597 } 2077 }
1598 size_l = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_ROOT_SIZE; 2078
1599 xi_l.value = (void *)&def_xv; 2079 if (rc) {
1600 xi_l.value_len = OCFS2_XATTR_ROOT_SIZE; 2080 ocfs2_xa_cleanup_value_truncate(loc, "reusing",
1601 } else if (xi->value) { 2081 orig_clusters);
1602 if (free < sizeof(struct ocfs2_xattr_entry) +
1603 OCFS2_XATTR_SIZE(name_len) +
1604 OCFS2_XATTR_SIZE(xi->value_len)) {
1605 ret = -ENOSPC;
1606 goto out; 2082 goto out;
1607 } 2083 }
1608 } 2084 }
1609 2085
1610 if (!xs->not_found) { 2086 loc->xl_entry->xe_value_size = cpu_to_le64(xi->xi_value_len);
1611 /* For existing extended attribute */ 2087 ocfs2_xattr_set_local(loc->xl_entry, xi_local);
1612 size_t size = OCFS2_XATTR_SIZE(name_len) +
1613 OCFS2_XATTR_SIZE(le64_to_cpu(xs->here->xe_value_size));
1614 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1615 void *val = xs->base + offs;
1616 2088
1617 if (ocfs2_xattr_is_local(xs->here) && size == size_l) { 2089out:
1618 /* Replace existing local xattr with tree root */ 2090 return rc;
1619 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, 2091}
1620 ctxt, &vb, offs);
1621 if (ret < 0)
1622 mlog_errno(ret);
1623 goto out;
1624 } else if (!ocfs2_xattr_is_local(xs->here)) {
1625 /* For existing xattr which has value outside */
1626 vb.vb_xv = (struct ocfs2_xattr_value_root *)
1627 (val + OCFS2_XATTR_SIZE(name_len));
1628 2092
1629 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { 2093/*
1630 /* 2094 * Prepares loc->xl_entry to receive the new xattr. This includes
1631 * If new value need set outside also, 2095 * properly setting up the name+value pair region. If loc->xl_entry
1632 * first truncate old value to new value, 2096 * already exists, it will take care of modifying it appropriately.
1633 * then set new value with set_value_outside(). 2097 *
1634 */ 2098 * Note that this modifies the data. You did journal_access already,
1635 ret = ocfs2_xattr_value_truncate(inode, 2099 * right?
1636 &vb, 2100 */
1637 xi->value_len, 2101static int ocfs2_xa_prepare_entry(struct ocfs2_xa_loc *loc,
1638 ctxt); 2102 struct ocfs2_xattr_info *xi,
1639 if (ret < 0) { 2103 u32 name_hash,
1640 mlog_errno(ret); 2104 struct ocfs2_xattr_set_ctxt *ctxt)
1641 goto out; 2105{
1642 } 2106 int rc = 0;
2107 unsigned int orig_clusters;
2108 __le64 orig_value_size = 0;
1643 2109
1644 ret = ocfs2_xattr_update_entry(inode, 2110 rc = ocfs2_xa_check_space(loc, xi);
1645 handle, 2111 if (rc)
1646 xi, 2112 goto out;
1647 xs,
1648 &vb,
1649 offs);
1650 if (ret < 0) {
1651 mlog_errno(ret);
1652 goto out;
1653 }
1654 2113
1655 ret = __ocfs2_xattr_set_value_outside(inode, 2114 if (loc->xl_entry) {
1656 handle, 2115 if (ocfs2_xa_can_reuse_entry(loc, xi)) {
1657 &vb, 2116 orig_value_size = loc->xl_entry->xe_value_size;
1658 xi->value, 2117 rc = ocfs2_xa_reuse_entry(loc, xi, ctxt);
1659 xi->value_len); 2118 if (rc)
1660 if (ret < 0) 2119 goto out;
1661 mlog_errno(ret); 2120 goto alloc_value;
2121 }
2122
2123 if (!ocfs2_xattr_is_local(loc->xl_entry)) {
2124 orig_clusters = ocfs2_xa_value_clusters(loc);
2125 rc = ocfs2_xa_value_truncate(loc, 0, ctxt);
2126 if (rc) {
2127 mlog_errno(rc);
2128 ocfs2_xa_cleanup_value_truncate(loc,
2129 "overwriting",
2130 orig_clusters);
1662 goto out; 2131 goto out;
1663 } else {
1664 /*
1665 * If new value need set in local,
1666 * just trucate old value to zero.
1667 */
1668 ret = ocfs2_xattr_value_truncate(inode,
1669 &vb,
1670 0,
1671 ctxt);
1672 if (ret < 0)
1673 mlog_errno(ret);
1674 } 2132 }
1675 } 2133 }
2134 ocfs2_xa_wipe_namevalue(loc);
2135 } else
2136 ocfs2_xa_add_entry(loc, name_hash);
2137
2138 /*
2139 * If we get here, we have a blank entry. Fill it. We grow our
2140 * name+value pair back from the end.
2141 */
2142 ocfs2_xa_add_namevalue(loc, xi);
2143 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2144 ocfs2_xa_install_value_root(loc);
2145
2146alloc_value:
2147 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2148 orig_clusters = ocfs2_xa_value_clusters(loc);
2149 rc = ocfs2_xa_value_truncate(loc, xi->xi_value_len, ctxt);
2150 if (rc < 0) {
2151 /*
2152 * If we tried to grow an existing external value,
2153 * ocfs2_xa_cleanuP-value_truncate() is going to
2154 * let it stand. We have to restore its original
2155 * value size.
2156 */
2157 loc->xl_entry->xe_value_size = orig_value_size;
2158 ocfs2_xa_cleanup_value_truncate(loc, "growing",
2159 orig_clusters);
2160 mlog_errno(rc);
2161 }
1676 } 2162 }
1677 2163
1678 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), xs->inode_bh, 2164out:
2165 return rc;
2166}
2167
2168/*
2169 * Store the value portion of the name+value pair. This will skip
2170 * values that are stored externally. Their tree roots were set up
2171 * by ocfs2_xa_prepare_entry().
2172 */
2173static int ocfs2_xa_store_value(struct ocfs2_xa_loc *loc,
2174 struct ocfs2_xattr_info *xi,
2175 struct ocfs2_xattr_set_ctxt *ctxt)
2176{
2177 int rc = 0;
2178 int nameval_offset = le16_to_cpu(loc->xl_entry->xe_name_offset);
2179 int name_size = OCFS2_XATTR_SIZE(xi->xi_name_len);
2180 char *nameval_buf;
2181 struct ocfs2_xattr_value_buf vb;
2182
2183 nameval_buf = ocfs2_xa_offset_pointer(loc, nameval_offset);
2184 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2185 ocfs2_xa_fill_value_buf(loc, &vb);
2186 rc = __ocfs2_xattr_set_value_outside(loc->xl_inode,
2187 ctxt->handle, &vb,
2188 xi->xi_value,
2189 xi->xi_value_len);
2190 } else
2191 memcpy(nameval_buf + name_size, xi->xi_value, xi->xi_value_len);
2192
2193 return rc;
2194}
2195
2196static int ocfs2_xa_set(struct ocfs2_xa_loc *loc,
2197 struct ocfs2_xattr_info *xi,
2198 struct ocfs2_xattr_set_ctxt *ctxt)
2199{
2200 int ret;
2201 u32 name_hash = ocfs2_xattr_name_hash(loc->xl_inode, xi->xi_name,
2202 xi->xi_name_len);
2203
2204 ret = ocfs2_xa_journal_access(ctxt->handle, loc,
1679 OCFS2_JOURNAL_ACCESS_WRITE); 2205 OCFS2_JOURNAL_ACCESS_WRITE);
1680 if (ret) { 2206 if (ret) {
1681 mlog_errno(ret); 2207 mlog_errno(ret);
1682 goto out; 2208 goto out;
1683 } 2209 }
1684 2210
1685 if (!(flag & OCFS2_INLINE_XATTR_FL)) {
1686 ret = vb.vb_access(handle, INODE_CACHE(inode), vb.vb_bh,
1687 OCFS2_JOURNAL_ACCESS_WRITE);
1688 if (ret) {
1689 mlog_errno(ret);
1690 goto out;
1691 }
1692 }
1693
1694 /* 2211 /*
1695 * Set value in local, include set tree root in local. 2212 * From here on out, everything is going to modify the buffer a
1696 * This is the first step for value size >INLINE_SIZE. 2213 * little. Errors are going to leave the xattr header in a
2214 * sane state. Thus, even with errors we dirty the sucker.
1697 */ 2215 */
1698 ocfs2_xattr_set_entry_local(inode, &xi_l, xs, last, min_offs);
1699 2216
1700 if (!(flag & OCFS2_INLINE_XATTR_FL)) { 2217 /* Don't worry, we are never called with !xi_value and !xl_entry */
1701 ret = ocfs2_journal_dirty(handle, xs->xattr_bh); 2218 if (!xi->xi_value) {
1702 if (ret < 0) { 2219 ret = ocfs2_xa_remove(loc, ctxt);
1703 mlog_errno(ret); 2220 goto out_dirty;
1704 goto out;
1705 }
1706 } 2221 }
1707 2222
1708 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL) && 2223 ret = ocfs2_xa_prepare_entry(loc, xi, name_hash, ctxt);
1709 (flag & OCFS2_INLINE_XATTR_FL)) { 2224 if (ret) {
1710 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 2225 if (ret != -ENOSPC)
1711 unsigned int xattrsize = osb->s_xattr_inline_size; 2226 mlog_errno(ret);
1712 2227 goto out_dirty;
1713 /*
1714 * Adjust extent record count or inline data size
1715 * to reserve space for extended attribute.
1716 */
1717 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1718 struct ocfs2_inline_data *idata = &di->id2.i_data;
1719 le16_add_cpu(&idata->id_count, -xattrsize);
1720 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
1721 struct ocfs2_extent_list *el = &di->id2.i_list;
1722 le16_add_cpu(&el->l_count, -(xattrsize /
1723 sizeof(struct ocfs2_extent_rec)));
1724 }
1725 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
1726 } 2228 }
1727 /* Update xattr flag */
1728 spin_lock(&oi->ip_lock);
1729 oi->ip_dyn_features |= flag;
1730 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
1731 spin_unlock(&oi->ip_lock);
1732 2229
1733 ret = ocfs2_journal_dirty(handle, xs->inode_bh); 2230 ret = ocfs2_xa_store_value(loc, xi, ctxt);
1734 if (ret < 0) 2231 if (ret)
1735 mlog_errno(ret); 2232 mlog_errno(ret);
1736 2233
1737 if (!ret && xi->value_len > OCFS2_XATTR_INLINE_SIZE) { 2234out_dirty:
1738 /* 2235 ocfs2_xa_journal_dirty(ctxt->handle, loc);
1739 * Set value outside in B tree.
1740 * This is the second step for value size > INLINE_SIZE.
1741 */
1742 size_t offs = le16_to_cpu(xs->here->xe_name_offset);
1743 ret = ocfs2_xattr_set_value_outside(inode, xi, xs, ctxt,
1744 &vb, offs);
1745 if (ret < 0) {
1746 int ret2;
1747 2236
1748 mlog_errno(ret);
1749 /*
1750 * If set value outside failed, we have to clean
1751 * the junk tree root we have already set in local.
1752 */
1753 ret2 = ocfs2_xattr_cleanup(inode, ctxt->handle,
1754 xi, xs, &vb, offs);
1755 if (ret2 < 0)
1756 mlog_errno(ret2);
1757 }
1758 }
1759out: 2237out:
1760 return ret; 2238 return ret;
1761} 2239}
1762 2240
2241static void ocfs2_init_dinode_xa_loc(struct ocfs2_xa_loc *loc,
2242 struct inode *inode,
2243 struct buffer_head *bh,
2244 struct ocfs2_xattr_entry *entry)
2245{
2246 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
2247
2248 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_XATTR_FL));
2249
2250 loc->xl_inode = inode;
2251 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2252 loc->xl_storage = bh;
2253 loc->xl_entry = entry;
2254 loc->xl_size = le16_to_cpu(di->i_xattr_inline_size);
2255 loc->xl_header =
2256 (struct ocfs2_xattr_header *)(bh->b_data + bh->b_size -
2257 loc->xl_size);
2258}
2259
2260static void ocfs2_init_xattr_block_xa_loc(struct ocfs2_xa_loc *loc,
2261 struct inode *inode,
2262 struct buffer_head *bh,
2263 struct ocfs2_xattr_entry *entry)
2264{
2265 struct ocfs2_xattr_block *xb =
2266 (struct ocfs2_xattr_block *)bh->b_data;
2267
2268 BUG_ON(le16_to_cpu(xb->xb_flags) & OCFS2_XATTR_INDEXED);
2269
2270 loc->xl_inode = inode;
2271 loc->xl_ops = &ocfs2_xa_block_loc_ops;
2272 loc->xl_storage = bh;
2273 loc->xl_header = &(xb->xb_attrs.xb_header);
2274 loc->xl_entry = entry;
2275 loc->xl_size = bh->b_size - offsetof(struct ocfs2_xattr_block,
2276 xb_attrs.xb_header);
2277}
2278
2279static void ocfs2_init_xattr_bucket_xa_loc(struct ocfs2_xa_loc *loc,
2280 struct ocfs2_xattr_bucket *bucket,
2281 struct ocfs2_xattr_entry *entry)
2282{
2283 loc->xl_inode = bucket->bu_inode;
2284 loc->xl_ops = &ocfs2_xa_bucket_loc_ops;
2285 loc->xl_storage = bucket;
2286 loc->xl_header = bucket_xh(bucket);
2287 loc->xl_entry = entry;
2288 loc->xl_size = OCFS2_XATTR_BUCKET_SIZE;
2289}
2290
1763/* 2291/*
1764 * In xattr remove, if it is stored outside and refcounted, we may have 2292 * In xattr remove, if it is stored outside and refcounted, we may have
1765 * the chance to split the refcount tree. So need the allocators. 2293 * the chance to split the refcount tree. So need the allocators.
@@ -2155,6 +2683,55 @@ static int ocfs2_xattr_ibody_find(struct inode *inode,
2155 return 0; 2683 return 0;
2156} 2684}
2157 2685
2686static int ocfs2_xattr_ibody_init(struct inode *inode,
2687 struct buffer_head *di_bh,
2688 struct ocfs2_xattr_set_ctxt *ctxt)
2689{
2690 int ret;
2691 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2692 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2693 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2694 unsigned int xattrsize = osb->s_xattr_inline_size;
2695
2696 if (!ocfs2_xattr_has_space_inline(inode, di)) {
2697 ret = -ENOSPC;
2698 goto out;
2699 }
2700
2701 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode), di_bh,
2702 OCFS2_JOURNAL_ACCESS_WRITE);
2703 if (ret) {
2704 mlog_errno(ret);
2705 goto out;
2706 }
2707
2708 /*
2709 * Adjust extent record count or inline data size
2710 * to reserve space for extended attribute.
2711 */
2712 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
2713 struct ocfs2_inline_data *idata = &di->id2.i_data;
2714 le16_add_cpu(&idata->id_count, -xattrsize);
2715 } else if (!(ocfs2_inode_is_fast_symlink(inode))) {
2716 struct ocfs2_extent_list *el = &di->id2.i_list;
2717 le16_add_cpu(&el->l_count, -(xattrsize /
2718 sizeof(struct ocfs2_extent_rec)));
2719 }
2720 di->i_xattr_inline_size = cpu_to_le16(xattrsize);
2721
2722 spin_lock(&oi->ip_lock);
2723 oi->ip_dyn_features |= OCFS2_INLINE_XATTR_FL|OCFS2_HAS_XATTR_FL;
2724 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
2725 spin_unlock(&oi->ip_lock);
2726
2727 ret = ocfs2_journal_dirty(ctxt->handle, di_bh);
2728 if (ret < 0)
2729 mlog_errno(ret);
2730
2731out:
2732 return ret;
2733}
2734
2158/* 2735/*
2159 * ocfs2_xattr_ibody_set() 2736 * ocfs2_xattr_ibody_set()
2160 * 2737 *
@@ -2166,9 +2743,10 @@ static int ocfs2_xattr_ibody_set(struct inode *inode,
2166 struct ocfs2_xattr_search *xs, 2743 struct ocfs2_xattr_search *xs,
2167 struct ocfs2_xattr_set_ctxt *ctxt) 2744 struct ocfs2_xattr_set_ctxt *ctxt)
2168{ 2745{
2746 int ret;
2169 struct ocfs2_inode_info *oi = OCFS2_I(inode); 2747 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2170 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data; 2748 struct ocfs2_dinode *di = (struct ocfs2_dinode *)xs->inode_bh->b_data;
2171 int ret; 2749 struct ocfs2_xa_loc loc;
2172 2750
2173 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE) 2751 if (inode->i_sb->s_blocksize == OCFS2_MIN_BLOCKSIZE)
2174 return -ENOSPC; 2752 return -ENOSPC;
@@ -2181,8 +2759,25 @@ static int ocfs2_xattr_ibody_set(struct inode *inode,
2181 } 2759 }
2182 } 2760 }
2183 2761
2184 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, 2762 if (!(oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
2185 (OCFS2_INLINE_XATTR_FL | OCFS2_HAS_XATTR_FL)); 2763 ret = ocfs2_xattr_ibody_init(inode, xs->inode_bh, ctxt);
2764 if (ret) {
2765 if (ret != -ENOSPC)
2766 mlog_errno(ret);
2767 goto out;
2768 }
2769 }
2770
2771 ocfs2_init_dinode_xa_loc(&loc, inode, xs->inode_bh,
2772 xs->not_found ? NULL : xs->here);
2773 ret = ocfs2_xa_set(&loc, xi, ctxt);
2774 if (ret) {
2775 if (ret != -ENOSPC)
2776 mlog_errno(ret);
2777 goto out;
2778 }
2779 xs->here = loc.xl_entry;
2780
2186out: 2781out:
2187 up_write(&oi->ip_alloc_sem); 2782 up_write(&oi->ip_alloc_sem);
2188 2783
@@ -2242,12 +2837,11 @@ cleanup:
2242 return ret; 2837 return ret;
2243} 2838}
2244 2839
2245static int ocfs2_create_xattr_block(handle_t *handle, 2840static int ocfs2_create_xattr_block(struct inode *inode,
2246 struct inode *inode,
2247 struct buffer_head *inode_bh, 2841 struct buffer_head *inode_bh,
2248 struct ocfs2_alloc_context *meta_ac, 2842 struct ocfs2_xattr_set_ctxt *ctxt,
2249 struct buffer_head **ret_bh, 2843 int indexed,
2250 int indexed) 2844 struct buffer_head **ret_bh)
2251{ 2845{
2252 int ret; 2846 int ret;
2253 u16 suballoc_bit_start; 2847 u16 suballoc_bit_start;
@@ -2258,14 +2852,14 @@ static int ocfs2_create_xattr_block(handle_t *handle,
2258 struct buffer_head *new_bh = NULL; 2852 struct buffer_head *new_bh = NULL;
2259 struct ocfs2_xattr_block *xblk; 2853 struct ocfs2_xattr_block *xblk;
2260 2854
2261 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), inode_bh, 2855 ret = ocfs2_journal_access_di(ctxt->handle, INODE_CACHE(inode),
2262 OCFS2_JOURNAL_ACCESS_CREATE); 2856 inode_bh, OCFS2_JOURNAL_ACCESS_CREATE);
2263 if (ret < 0) { 2857 if (ret < 0) {
2264 mlog_errno(ret); 2858 mlog_errno(ret);
2265 goto end; 2859 goto end;
2266 } 2860 }
2267 2861
2268 ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1, 2862 ret = ocfs2_claim_metadata(osb, ctxt->handle, ctxt->meta_ac, 1,
2269 &suballoc_bit_start, &num_got, 2863 &suballoc_bit_start, &num_got,
2270 &first_blkno); 2864 &first_blkno);
2271 if (ret < 0) { 2865 if (ret < 0) {
@@ -2276,7 +2870,7 @@ static int ocfs2_create_xattr_block(handle_t *handle,
2276 new_bh = sb_getblk(inode->i_sb, first_blkno); 2870 new_bh = sb_getblk(inode->i_sb, first_blkno);
2277 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh); 2871 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode), new_bh);
2278 2872
2279 ret = ocfs2_journal_access_xb(handle, INODE_CACHE(inode), 2873 ret = ocfs2_journal_access_xb(ctxt->handle, INODE_CACHE(inode),
2280 new_bh, 2874 new_bh,
2281 OCFS2_JOURNAL_ACCESS_CREATE); 2875 OCFS2_JOURNAL_ACCESS_CREATE);
2282 if (ret < 0) { 2876 if (ret < 0) {
@@ -2288,11 +2882,10 @@ static int ocfs2_create_xattr_block(handle_t *handle,
2288 xblk = (struct ocfs2_xattr_block *)new_bh->b_data; 2882 xblk = (struct ocfs2_xattr_block *)new_bh->b_data;
2289 memset(xblk, 0, inode->i_sb->s_blocksize); 2883 memset(xblk, 0, inode->i_sb->s_blocksize);
2290 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE); 2884 strcpy((void *)xblk, OCFS2_XATTR_BLOCK_SIGNATURE);
2291 xblk->xb_suballoc_slot = cpu_to_le16(osb->slot_num); 2885 xblk->xb_suballoc_slot = cpu_to_le16(ctxt->meta_ac->ac_alloc_slot);
2292 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start); 2886 xblk->xb_suballoc_bit = cpu_to_le16(suballoc_bit_start);
2293 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation); 2887 xblk->xb_fs_generation = cpu_to_le32(osb->fs_generation);
2294 xblk->xb_blkno = cpu_to_le64(first_blkno); 2888 xblk->xb_blkno = cpu_to_le64(first_blkno);
2295
2296 if (indexed) { 2889 if (indexed) {
2297 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root; 2890 struct ocfs2_xattr_tree_root *xr = &xblk->xb_attrs.xb_root;
2298 xr->xt_clusters = cpu_to_le32(1); 2891 xr->xt_clusters = cpu_to_le32(1);
@@ -2303,14 +2896,17 @@ static int ocfs2_create_xattr_block(handle_t *handle,
2303 xr->xt_list.l_next_free_rec = cpu_to_le16(1); 2896 xr->xt_list.l_next_free_rec = cpu_to_le16(1);
2304 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED); 2897 xblk->xb_flags = cpu_to_le16(OCFS2_XATTR_INDEXED);
2305 } 2898 }
2899 ocfs2_journal_dirty(ctxt->handle, new_bh);
2306 2900
2307 ret = ocfs2_journal_dirty(handle, new_bh); 2901 /* Add it to the inode */
2308 if (ret < 0) {
2309 mlog_errno(ret);
2310 goto end;
2311 }
2312 di->i_xattr_loc = cpu_to_le64(first_blkno); 2902 di->i_xattr_loc = cpu_to_le64(first_blkno);
2313 ocfs2_journal_dirty(handle, inode_bh); 2903
2904 spin_lock(&OCFS2_I(inode)->ip_lock);
2905 OCFS2_I(inode)->ip_dyn_features |= OCFS2_HAS_XATTR_FL;
2906 di->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features);
2907 spin_unlock(&OCFS2_I(inode)->ip_lock);
2908
2909 ocfs2_journal_dirty(ctxt->handle, inode_bh);
2314 2910
2315 *ret_bh = new_bh; 2911 *ret_bh = new_bh;
2316 new_bh = NULL; 2912 new_bh = NULL;
@@ -2332,13 +2928,13 @@ static int ocfs2_xattr_block_set(struct inode *inode,
2332 struct ocfs2_xattr_set_ctxt *ctxt) 2928 struct ocfs2_xattr_set_ctxt *ctxt)
2333{ 2929{
2334 struct buffer_head *new_bh = NULL; 2930 struct buffer_head *new_bh = NULL;
2335 handle_t *handle = ctxt->handle;
2336 struct ocfs2_xattr_block *xblk = NULL; 2931 struct ocfs2_xattr_block *xblk = NULL;
2337 int ret; 2932 int ret;
2933 struct ocfs2_xa_loc loc;
2338 2934
2339 if (!xs->xattr_bh) { 2935 if (!xs->xattr_bh) {
2340 ret = ocfs2_create_xattr_block(handle, inode, xs->inode_bh, 2936 ret = ocfs2_create_xattr_block(inode, xs->inode_bh, ctxt,
2341 ctxt->meta_ac, &new_bh, 0); 2937 0, &new_bh);
2342 if (ret) { 2938 if (ret) {
2343 mlog_errno(ret); 2939 mlog_errno(ret);
2344 goto end; 2940 goto end;
@@ -2354,21 +2950,25 @@ static int ocfs2_xattr_block_set(struct inode *inode,
2354 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data; 2950 xblk = (struct ocfs2_xattr_block *)xs->xattr_bh->b_data;
2355 2951
2356 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) { 2952 if (!(le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)) {
2357 /* Set extended attribute into external block */ 2953 ocfs2_init_xattr_block_xa_loc(&loc, inode, xs->xattr_bh,
2358 ret = ocfs2_xattr_set_entry(inode, xi, xs, ctxt, 2954 xs->not_found ? NULL : xs->here);
2359 OCFS2_HAS_XATTR_FL);
2360 if (!ret || ret != -ENOSPC)
2361 goto end;
2362 2955
2363 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt); 2956 ret = ocfs2_xa_set(&loc, xi, ctxt);
2364 if (ret) 2957 if (!ret)
2958 xs->here = loc.xl_entry;
2959 else if (ret != -ENOSPC)
2365 goto end; 2960 goto end;
2961 else {
2962 ret = ocfs2_xattr_create_index_block(inode, xs, ctxt);
2963 if (ret)
2964 goto end;
2965 }
2366 } 2966 }
2367 2967
2368 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt); 2968 if (le16_to_cpu(xblk->xb_flags) & OCFS2_XATTR_INDEXED)
2969 ret = ocfs2_xattr_set_entry_index_block(inode, xi, xs, ctxt);
2369 2970
2370end: 2971end:
2371
2372 return ret; 2972 return ret;
2373} 2973}
2374 2974
@@ -2377,7 +2977,6 @@ static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2377 struct ocfs2_xattr_info *xi, 2977 struct ocfs2_xattr_info *xi,
2378 struct ocfs2_xattr_search *xs) 2978 struct ocfs2_xattr_search *xs)
2379{ 2979{
2380 u64 value_size;
2381 struct ocfs2_xattr_entry *last; 2980 struct ocfs2_xattr_entry *last;
2382 int free, i; 2981 int free, i;
2383 size_t min_offs = xs->end - xs->base; 2982 size_t min_offs = xs->end - xs->base;
@@ -2400,13 +2999,7 @@ static int ocfs2_xattr_can_be_in_inode(struct inode *inode,
2400 2999
2401 BUG_ON(!xs->not_found); 3000 BUG_ON(!xs->not_found);
2402 3001
2403 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) 3002 if (free >= (sizeof(struct ocfs2_xattr_entry) + namevalue_size_xi(xi)))
2404 value_size = OCFS2_XATTR_ROOT_SIZE;
2405 else
2406 value_size = OCFS2_XATTR_SIZE(xi->value_len);
2407
2408 if (free >= sizeof(struct ocfs2_xattr_entry) +
2409 OCFS2_XATTR_SIZE(strlen(xi->name)) + value_size)
2410 return 1; 3003 return 1;
2411 3004
2412 return 0; 3005 return 0;
@@ -2430,7 +3023,7 @@ static int ocfs2_calc_xattr_set_need(struct inode *inode,
2430 char *base = NULL; 3023 char *base = NULL;
2431 int name_offset, name_len = 0; 3024 int name_offset, name_len = 0;
2432 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb, 3025 u32 new_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
2433 xi->value_len); 3026 xi->xi_value_len);
2434 u64 value_size; 3027 u64 value_size;
2435 3028
2436 /* 3029 /*
@@ -2438,14 +3031,14 @@ static int ocfs2_calc_xattr_set_need(struct inode *inode,
2438 * No matter whether we replace an old one or add a new one, 3031 * No matter whether we replace an old one or add a new one,
2439 * we need this for writing. 3032 * we need this for writing.
2440 */ 3033 */
2441 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) 3034 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE)
2442 credits += new_clusters * 3035 credits += new_clusters *
2443 ocfs2_clusters_to_blocks(inode->i_sb, 1); 3036 ocfs2_clusters_to_blocks(inode->i_sb, 1);
2444 3037
2445 if (xis->not_found && xbs->not_found) { 3038 if (xis->not_found && xbs->not_found) {
2446 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb); 3039 credits += ocfs2_blocks_per_xattr_bucket(inode->i_sb);
2447 3040
2448 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { 3041 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2449 clusters_add += new_clusters; 3042 clusters_add += new_clusters;
2450 credits += ocfs2_calc_extend_credits(inode->i_sb, 3043 credits += ocfs2_calc_extend_credits(inode->i_sb,
2451 &def_xv.xv.xr_list, 3044 &def_xv.xv.xr_list,
@@ -2490,7 +3083,7 @@ static int ocfs2_calc_xattr_set_need(struct inode *inode,
2490 * The credits for removing the value tree will be extended 3083 * The credits for removing the value tree will be extended
2491 * by ocfs2_remove_extent itself. 3084 * by ocfs2_remove_extent itself.
2492 */ 3085 */
2493 if (!xi->value) { 3086 if (!xi->xi_value) {
2494 if (!ocfs2_xattr_is_local(xe)) 3087 if (!ocfs2_xattr_is_local(xe))
2495 credits += ocfs2_remove_extent_credits(inode->i_sb); 3088 credits += ocfs2_remove_extent_credits(inode->i_sb);
2496 3089
@@ -2520,7 +3113,7 @@ static int ocfs2_calc_xattr_set_need(struct inode *inode,
2520 } 3113 }
2521 } 3114 }
2522 3115
2523 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE) { 3116 if (xi->xi_value_len > OCFS2_XATTR_INLINE_SIZE) {
2524 /* the new values will be stored outside. */ 3117 /* the new values will be stored outside. */
2525 u32 old_clusters = 0; 3118 u32 old_clusters = 0;
2526 3119
@@ -2553,9 +3146,10 @@ static int ocfs2_calc_xattr_set_need(struct inode *inode,
2553 * value, we don't need any allocation, otherwise we have 3146 * value, we don't need any allocation, otherwise we have
2554 * to guess metadata allocation. 3147 * to guess metadata allocation.
2555 */ 3148 */
2556 if ((ocfs2_xattr_is_local(xe) && value_size >= xi->value_len) || 3149 if ((ocfs2_xattr_is_local(xe) &&
3150 (value_size >= xi->xi_value_len)) ||
2557 (!ocfs2_xattr_is_local(xe) && 3151 (!ocfs2_xattr_is_local(xe) &&
2558 OCFS2_XATTR_ROOT_SIZE >= xi->value_len)) 3152 OCFS2_XATTR_ROOT_SIZE >= xi->xi_value_len))
2559 goto out; 3153 goto out;
2560 } 3154 }
2561 3155
@@ -2645,7 +3239,7 @@ static int ocfs2_init_xattr_set_ctxt(struct inode *inode,
2645 3239
2646 meta_add += extra_meta; 3240 meta_add += extra_meta;
2647 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, " 3241 mlog(0, "Set xattr %s, reserve meta blocks = %d, clusters = %d, "
2648 "credits = %d\n", xi->name, meta_add, clusters_add, *credits); 3242 "credits = %d\n", xi->xi_name, meta_add, clusters_add, *credits);
2649 3243
2650 if (meta_add) { 3244 if (meta_add) {
2651 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add, 3245 ret = ocfs2_reserve_new_metadata_blocks(osb, meta_add,
@@ -2685,7 +3279,7 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
2685{ 3279{
2686 int ret = 0, credits, old_found; 3280 int ret = 0, credits, old_found;
2687 3281
2688 if (!xi->value) { 3282 if (!xi->xi_value) {
2689 /* Remove existing extended attribute */ 3283 /* Remove existing extended attribute */
2690 if (!xis->not_found) 3284 if (!xis->not_found)
2691 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt); 3285 ret = ocfs2_xattr_ibody_set(inode, xi, xis, ctxt);
@@ -2699,8 +3293,8 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
2699 * If succeed and that extended attribute existing in 3293 * If succeed and that extended attribute existing in
2700 * external block, then we will remove it. 3294 * external block, then we will remove it.
2701 */ 3295 */
2702 xi->value = NULL; 3296 xi->xi_value = NULL;
2703 xi->value_len = 0; 3297 xi->xi_value_len = 0;
2704 3298
2705 old_found = xis->not_found; 3299 old_found = xis->not_found;
2706 xis->not_found = -ENODATA; 3300 xis->not_found = -ENODATA;
@@ -2728,8 +3322,8 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
2728 } else if (ret == -ENOSPC) { 3322 } else if (ret == -ENOSPC) {
2729 if (di->i_xattr_loc && !xbs->xattr_bh) { 3323 if (di->i_xattr_loc && !xbs->xattr_bh) {
2730 ret = ocfs2_xattr_block_find(inode, 3324 ret = ocfs2_xattr_block_find(inode,
2731 xi->name_index, 3325 xi->xi_name_index,
2732 xi->name, xbs); 3326 xi->xi_name, xbs);
2733 if (ret) 3327 if (ret)
2734 goto out; 3328 goto out;
2735 3329
@@ -2768,8 +3362,8 @@ static int __ocfs2_xattr_set_handle(struct inode *inode,
2768 * If succeed and that extended attribute 3362 * If succeed and that extended attribute
2769 * existing in inode, we will remove it. 3363 * existing in inode, we will remove it.
2770 */ 3364 */
2771 xi->value = NULL; 3365 xi->xi_value = NULL;
2772 xi->value_len = 0; 3366 xi->xi_value_len = 0;
2773 xbs->not_found = -ENODATA; 3367 xbs->not_found = -ENODATA;
2774 ret = ocfs2_calc_xattr_set_need(inode, 3368 ret = ocfs2_calc_xattr_set_need(inode,
2775 di, 3369 di,
@@ -2835,10 +3429,11 @@ int ocfs2_xattr_set_handle(handle_t *handle,
2835 int ret; 3429 int ret;
2836 3430
2837 struct ocfs2_xattr_info xi = { 3431 struct ocfs2_xattr_info xi = {
2838 .name_index = name_index, 3432 .xi_name_index = name_index,
2839 .name = name, 3433 .xi_name = name,
2840 .value = value, 3434 .xi_name_len = strlen(name),
2841 .value_len = value_len, 3435 .xi_value = value,
3436 .xi_value_len = value_len,
2842 }; 3437 };
2843 3438
2844 struct ocfs2_xattr_search xis = { 3439 struct ocfs2_xattr_search xis = {
@@ -2918,10 +3513,11 @@ int ocfs2_xattr_set(struct inode *inode,
2918 struct ocfs2_refcount_tree *ref_tree = NULL; 3513 struct ocfs2_refcount_tree *ref_tree = NULL;
2919 3514
2920 struct ocfs2_xattr_info xi = { 3515 struct ocfs2_xattr_info xi = {
2921 .name_index = name_index, 3516 .xi_name_index = name_index,
2922 .name = name, 3517 .xi_name = name,
2923 .value = value, 3518 .xi_name_len = strlen(name),
2924 .value_len = value_len, 3519 .xi_value = value,
3520 .xi_value_len = value_len,
2925 }; 3521 };
2926 3522
2927 struct ocfs2_xattr_search xis = { 3523 struct ocfs2_xattr_search xis = {
@@ -3765,7 +4361,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3765 struct ocfs2_xattr_bucket *bucket) 4361 struct ocfs2_xattr_bucket *bucket)
3766{ 4362{
3767 int ret, i; 4363 int ret, i;
3768 size_t end, offset, len, value_len; 4364 size_t end, offset, len;
3769 struct ocfs2_xattr_header *xh; 4365 struct ocfs2_xattr_header *xh;
3770 char *entries, *buf, *bucket_buf = NULL; 4366 char *entries, *buf, *bucket_buf = NULL;
3771 u64 blkno = bucket_blkno(bucket); 4367 u64 blkno = bucket_blkno(bucket);
@@ -3819,12 +4415,7 @@ static int ocfs2_defrag_xattr_bucket(struct inode *inode,
3819 end = OCFS2_XATTR_BUCKET_SIZE; 4415 end = OCFS2_XATTR_BUCKET_SIZE;
3820 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) { 4416 for (i = 0; i < le16_to_cpu(xh->xh_count); i++, xe++) {
3821 offset = le16_to_cpu(xe->xe_name_offset); 4417 offset = le16_to_cpu(xe->xe_name_offset);
3822 if (ocfs2_xattr_is_local(xe)) 4418 len = namevalue_size_xe(xe);
3823 value_len = OCFS2_XATTR_SIZE(
3824 le64_to_cpu(xe->xe_value_size));
3825 else
3826 value_len = OCFS2_XATTR_ROOT_SIZE;
3827 len = OCFS2_XATTR_SIZE(xe->xe_name_len) + value_len;
3828 4419
3829 /* 4420 /*
3830 * We must make sure that the name/value pair 4421 * We must make sure that the name/value pair
@@ -4013,7 +4604,7 @@ static int ocfs2_divide_xattr_bucket(struct inode *inode,
4013 int new_bucket_head) 4604 int new_bucket_head)
4014{ 4605{
4015 int ret, i; 4606 int ret, i;
4016 int count, start, len, name_value_len = 0, xe_len, name_offset = 0; 4607 int count, start, len, name_value_len = 0, name_offset = 0;
4017 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL; 4608 struct ocfs2_xattr_bucket *s_bucket = NULL, *t_bucket = NULL;
4018 struct ocfs2_xattr_header *xh; 4609 struct ocfs2_xattr_header *xh;
4019 struct ocfs2_xattr_entry *xe; 4610 struct ocfs2_xattr_entry *xe;
@@ -4104,13 +4695,7 @@ static int ocfs2_divide_xattr_bucket(struct inode *inode,
4104 name_value_len = 0; 4695 name_value_len = 0;
4105 for (i = 0; i < start; i++) { 4696 for (i = 0; i < start; i++) {
4106 xe = &xh->xh_entries[i]; 4697 xe = &xh->xh_entries[i];
4107 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len); 4698 name_value_len += namevalue_size_xe(xe);
4108 if (ocfs2_xattr_is_local(xe))
4109 xe_len +=
4110 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4111 else
4112 xe_len += OCFS2_XATTR_ROOT_SIZE;
4113 name_value_len += xe_len;
4114 if (le16_to_cpu(xe->xe_name_offset) < name_offset) 4699 if (le16_to_cpu(xe->xe_name_offset) < name_offset)
4115 name_offset = le16_to_cpu(xe->xe_name_offset); 4700 name_offset = le16_to_cpu(xe->xe_name_offset);
4116 } 4701 }
@@ -4140,12 +4725,6 @@ static int ocfs2_divide_xattr_bucket(struct inode *inode,
4140 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE); 4725 xh->xh_free_start = cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4141 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) { 4726 for (i = 0; i < le16_to_cpu(xh->xh_count); i++) {
4142 xe = &xh->xh_entries[i]; 4727 xe = &xh->xh_entries[i];
4143 xe_len = OCFS2_XATTR_SIZE(xe->xe_name_len);
4144 if (ocfs2_xattr_is_local(xe))
4145 xe_len +=
4146 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4147 else
4148 xe_len += OCFS2_XATTR_ROOT_SIZE;
4149 if (le16_to_cpu(xe->xe_name_offset) < 4728 if (le16_to_cpu(xe->xe_name_offset) <
4150 le16_to_cpu(xh->xh_free_start)) 4729 le16_to_cpu(xh->xh_free_start))
4151 xh->xh_free_start = xe->xe_name_offset; 4730 xh->xh_free_start = xe->xe_name_offset;
@@ -4757,195 +5336,6 @@ static inline char *ocfs2_xattr_bucket_get_val(struct inode *inode,
4757} 5336}
4758 5337
4759/* 5338/*
4760 * Handle the normal xattr set, including replace, delete and new.
4761 *
4762 * Note: "local" indicates the real data's locality. So we can't
4763 * just its bucket locality by its length.
4764 */
4765static void ocfs2_xattr_set_entry_normal(struct inode *inode,
4766 struct ocfs2_xattr_info *xi,
4767 struct ocfs2_xattr_search *xs,
4768 u32 name_hash,
4769 int local)
4770{
4771 struct ocfs2_xattr_entry *last, *xe;
4772 int name_len = strlen(xi->name);
4773 struct ocfs2_xattr_header *xh = xs->header;
4774 u16 count = le16_to_cpu(xh->xh_count), start;
4775 size_t blocksize = inode->i_sb->s_blocksize;
4776 char *val;
4777 size_t offs, size, new_size;
4778
4779 last = &xh->xh_entries[count];
4780 if (!xs->not_found) {
4781 xe = xs->here;
4782 offs = le16_to_cpu(xe->xe_name_offset);
4783 if (ocfs2_xattr_is_local(xe))
4784 size = OCFS2_XATTR_SIZE(name_len) +
4785 OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
4786 else
4787 size = OCFS2_XATTR_SIZE(name_len) +
4788 OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
4789
4790 /*
4791 * If the new value will be stored outside, xi->value has been
4792 * initalized as an empty ocfs2_xattr_value_root, and the same
4793 * goes with xi->value_len, so we can set new_size safely here.
4794 * See ocfs2_xattr_set_in_bucket.
4795 */
4796 new_size = OCFS2_XATTR_SIZE(name_len) +
4797 OCFS2_XATTR_SIZE(xi->value_len);
4798
4799 le16_add_cpu(&xh->xh_name_value_len, -size);
4800 if (xi->value) {
4801 if (new_size > size)
4802 goto set_new_name_value;
4803
4804 /* Now replace the old value with new one. */
4805 if (local)
4806 xe->xe_value_size = cpu_to_le64(xi->value_len);
4807 else
4808 xe->xe_value_size = 0;
4809
4810 val = ocfs2_xattr_bucket_get_val(inode,
4811 xs->bucket, offs);
4812 memset(val + OCFS2_XATTR_SIZE(name_len), 0,
4813 size - OCFS2_XATTR_SIZE(name_len));
4814 if (OCFS2_XATTR_SIZE(xi->value_len) > 0)
4815 memcpy(val + OCFS2_XATTR_SIZE(name_len),
4816 xi->value, xi->value_len);
4817
4818 le16_add_cpu(&xh->xh_name_value_len, new_size);
4819 ocfs2_xattr_set_local(xe, local);
4820 return;
4821 } else {
4822 /*
4823 * Remove the old entry if there is more than one.
4824 * We don't remove the last entry so that we can
4825 * use it to indicate the hash value of the empty
4826 * bucket.
4827 */
4828 last -= 1;
4829 le16_add_cpu(&xh->xh_count, -1);
4830 if (xh->xh_count) {
4831 memmove(xe, xe + 1,
4832 (void *)last - (void *)xe);
4833 memset(last, 0,
4834 sizeof(struct ocfs2_xattr_entry));
4835 } else
4836 xh->xh_free_start =
4837 cpu_to_le16(OCFS2_XATTR_BUCKET_SIZE);
4838
4839 return;
4840 }
4841 } else {
4842 /* find a new entry for insert. */
4843 int low = 0, high = count - 1, tmp;
4844 struct ocfs2_xattr_entry *tmp_xe;
4845
4846 while (low <= high && count) {
4847 tmp = (low + high) / 2;
4848 tmp_xe = &xh->xh_entries[tmp];
4849
4850 if (name_hash > le32_to_cpu(tmp_xe->xe_name_hash))
4851 low = tmp + 1;
4852 else if (name_hash <
4853 le32_to_cpu(tmp_xe->xe_name_hash))
4854 high = tmp - 1;
4855 else {
4856 low = tmp;
4857 break;
4858 }
4859 }
4860
4861 xe = &xh->xh_entries[low];
4862 if (low != count)
4863 memmove(xe + 1, xe, (void *)last - (void *)xe);
4864
4865 le16_add_cpu(&xh->xh_count, 1);
4866 memset(xe, 0, sizeof(struct ocfs2_xattr_entry));
4867 xe->xe_name_hash = cpu_to_le32(name_hash);
4868 xe->xe_name_len = name_len;
4869 ocfs2_xattr_set_type(xe, xi->name_index);
4870 }
4871
4872set_new_name_value:
4873 /* Insert the new name+value. */
4874 size = OCFS2_XATTR_SIZE(name_len) + OCFS2_XATTR_SIZE(xi->value_len);
4875
4876 /*
4877 * We must make sure that the name/value pair
4878 * exists in the same block.
4879 */
4880 offs = le16_to_cpu(xh->xh_free_start);
4881 start = offs - size;
4882
4883 if (start >> inode->i_sb->s_blocksize_bits !=
4884 (offs - 1) >> inode->i_sb->s_blocksize_bits) {
4885 offs = offs - offs % blocksize;
4886 xh->xh_free_start = cpu_to_le16(offs);
4887 }
4888
4889 val = ocfs2_xattr_bucket_get_val(inode, xs->bucket, offs - size);
4890 xe->xe_name_offset = cpu_to_le16(offs - size);
4891
4892 memset(val, 0, size);
4893 memcpy(val, xi->name, name_len);
4894 memcpy(val + OCFS2_XATTR_SIZE(name_len), xi->value, xi->value_len);
4895
4896 xe->xe_value_size = cpu_to_le64(xi->value_len);
4897 ocfs2_xattr_set_local(xe, local);
4898 xs->here = xe;
4899 le16_add_cpu(&xh->xh_free_start, -size);
4900 le16_add_cpu(&xh->xh_name_value_len, size);
4901
4902 return;
4903}
4904
4905/*
4906 * Set the xattr entry in the specified bucket.
4907 * The bucket is indicated by xs->bucket and it should have the enough
4908 * space for the xattr insertion.
4909 */
4910static int ocfs2_xattr_set_entry_in_bucket(struct inode *inode,
4911 handle_t *handle,
4912 struct ocfs2_xattr_info *xi,
4913 struct ocfs2_xattr_search *xs,
4914 u32 name_hash,
4915 int local)
4916{
4917 int ret;
4918 u64 blkno;
4919
4920 mlog(0, "Set xattr entry len = %lu index = %d in bucket %llu\n",
4921 (unsigned long)xi->value_len, xi->name_index,
4922 (unsigned long long)bucket_blkno(xs->bucket));
4923
4924 if (!xs->bucket->bu_bhs[1]) {
4925 blkno = bucket_blkno(xs->bucket);
4926 ocfs2_xattr_bucket_relse(xs->bucket);
4927 ret = ocfs2_read_xattr_bucket(xs->bucket, blkno);
4928 if (ret) {
4929 mlog_errno(ret);
4930 goto out;
4931 }
4932 }
4933
4934 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
4935 OCFS2_JOURNAL_ACCESS_WRITE);
4936 if (ret < 0) {
4937 mlog_errno(ret);
4938 goto out;
4939 }
4940
4941 ocfs2_xattr_set_entry_normal(inode, xi, xs, name_hash, local);
4942 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
4943
4944out:
4945 return ret;
4946}
4947
4948/*
4949 * Truncate the specified xe_off entry in xattr bucket. 5339 * Truncate the specified xe_off entry in xattr bucket.
4950 * bucket is indicated by header_bh and len is the new length. 5340 * bucket is indicated by header_bh and len is the new length.
4951 * Both the ocfs2_xattr_value_root and the entry will be updated here. 5341 * Both the ocfs2_xattr_value_root and the entry will be updated here.
@@ -5015,66 +5405,6 @@ out:
5015 return ret; 5405 return ret;
5016} 5406}
5017 5407
5018static int ocfs2_xattr_bucket_value_truncate_xs(struct inode *inode,
5019 struct ocfs2_xattr_search *xs,
5020 int len,
5021 struct ocfs2_xattr_set_ctxt *ctxt)
5022{
5023 int ret, offset;
5024 struct ocfs2_xattr_entry *xe = xs->here;
5025 struct ocfs2_xattr_header *xh = (struct ocfs2_xattr_header *)xs->base;
5026
5027 BUG_ON(!xs->bucket->bu_bhs[0] || !xe || ocfs2_xattr_is_local(xe));
5028
5029 offset = xe - xh->xh_entries;
5030 ret = ocfs2_xattr_bucket_value_truncate(inode, xs->bucket,
5031 offset, len, ctxt);
5032 if (ret)
5033 mlog_errno(ret);
5034
5035 return ret;
5036}
5037
5038static int ocfs2_xattr_bucket_set_value_outside(struct inode *inode,
5039 handle_t *handle,
5040 struct ocfs2_xattr_search *xs,
5041 char *val,
5042 int value_len)
5043{
5044 int ret, offset, block_off;
5045 struct ocfs2_xattr_value_root *xv;
5046 struct ocfs2_xattr_entry *xe = xs->here;
5047 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5048 void *base;
5049 struct ocfs2_xattr_value_buf vb = {
5050 .vb_access = ocfs2_journal_access,
5051 };
5052
5053 BUG_ON(!xs->base || !xe || ocfs2_xattr_is_local(xe));
5054
5055 ret = ocfs2_xattr_bucket_get_name_value(inode->i_sb, xh,
5056 xe - xh->xh_entries,
5057 &block_off,
5058 &offset);
5059 if (ret) {
5060 mlog_errno(ret);
5061 goto out;
5062 }
5063
5064 base = bucket_block(xs->bucket, block_off);
5065 xv = (struct ocfs2_xattr_value_root *)(base + offset +
5066 OCFS2_XATTR_SIZE(xe->xe_name_len));
5067
5068 vb.vb_xv = xv;
5069 vb.vb_bh = xs->bucket->bu_bhs[block_off];
5070 ret = __ocfs2_xattr_set_value_outside(inode, handle,
5071 &vb, val, value_len);
5072 if (ret)
5073 mlog_errno(ret);
5074out:
5075 return ret;
5076}
5077
5078static int ocfs2_rm_xattr_cluster(struct inode *inode, 5408static int ocfs2_rm_xattr_cluster(struct inode *inode,
5079 struct buffer_head *root_bh, 5409 struct buffer_head *root_bh,
5080 u64 blkno, 5410 u64 blkno,
@@ -5173,128 +5503,6 @@ out:
5173 return ret; 5503 return ret;
5174} 5504}
5175 5505
5176static void ocfs2_xattr_bucket_remove_xs(struct inode *inode,
5177 handle_t *handle,
5178 struct ocfs2_xattr_search *xs)
5179{
5180 struct ocfs2_xattr_header *xh = bucket_xh(xs->bucket);
5181 struct ocfs2_xattr_entry *last = &xh->xh_entries[
5182 le16_to_cpu(xh->xh_count) - 1];
5183 int ret = 0;
5184
5185 ret = ocfs2_xattr_bucket_journal_access(handle, xs->bucket,
5186 OCFS2_JOURNAL_ACCESS_WRITE);
5187 if (ret) {
5188 mlog_errno(ret);
5189 return;
5190 }
5191
5192 /* Remove the old entry. */
5193 memmove(xs->here, xs->here + 1,
5194 (void *)last - (void *)xs->here);
5195 memset(last, 0, sizeof(struct ocfs2_xattr_entry));
5196 le16_add_cpu(&xh->xh_count, -1);
5197
5198 ocfs2_xattr_bucket_journal_dirty(handle, xs->bucket);
5199}
5200
5201/*
5202 * Set the xattr name/value in the bucket specified in xs.
5203 *
5204 * As the new value in xi may be stored in the bucket or in an outside cluster,
5205 * we divide the whole process into 3 steps:
5206 * 1. insert name/value in the bucket(ocfs2_xattr_set_entry_in_bucket)
5207 * 2. truncate of the outside cluster(ocfs2_xattr_bucket_value_truncate_xs)
5208 * 3. Set the value to the outside cluster(ocfs2_xattr_bucket_set_value_outside)
5209 * 4. If the clusters for the new outside value can't be allocated, we need
5210 * to free the xattr we allocated in set.
5211 */
5212static int ocfs2_xattr_set_in_bucket(struct inode *inode,
5213 struct ocfs2_xattr_info *xi,
5214 struct ocfs2_xattr_search *xs,
5215 struct ocfs2_xattr_set_ctxt *ctxt)
5216{
5217 int ret, local = 1;
5218 size_t value_len;
5219 char *val = (char *)xi->value;
5220 struct ocfs2_xattr_entry *xe = xs->here;
5221 u32 name_hash = ocfs2_xattr_name_hash(inode, xi->name,
5222 strlen(xi->name));
5223
5224 if (!xs->not_found && !ocfs2_xattr_is_local(xe)) {
5225 /*
5226 * We need to truncate the xattr storage first.
5227 *
5228 * If both the old and new value are stored to
5229 * outside block, we only need to truncate
5230 * the storage and then set the value outside.
5231 *
5232 * If the new value should be stored within block,
5233 * we should free all the outside block first and
5234 * the modification to the xattr block will be done
5235 * by following steps.
5236 */
5237 if (xi->value_len > OCFS2_XATTR_INLINE_SIZE)
5238 value_len = xi->value_len;
5239 else
5240 value_len = 0;
5241
5242 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5243 value_len,
5244 ctxt);
5245 if (ret)
5246 goto out;
5247
5248 if (value_len)
5249 goto set_value_outside;
5250 }
5251
5252 value_len = xi->value_len;
5253 /* So we have to handle the inside block change now. */
5254 if (value_len > OCFS2_XATTR_INLINE_SIZE) {
5255 /*
5256 * If the new value will be stored outside of block,
5257 * initalize a new empty value root and insert it first.
5258 */
5259 local = 0;
5260 xi->value = &def_xv;
5261 xi->value_len = OCFS2_XATTR_ROOT_SIZE;
5262 }
5263
5264 ret = ocfs2_xattr_set_entry_in_bucket(inode, ctxt->handle, xi, xs,
5265 name_hash, local);
5266 if (ret) {
5267 mlog_errno(ret);
5268 goto out;
5269 }
5270
5271 if (value_len <= OCFS2_XATTR_INLINE_SIZE)
5272 goto out;
5273
5274 /* allocate the space now for the outside block storage. */
5275 ret = ocfs2_xattr_bucket_value_truncate_xs(inode, xs,
5276 value_len, ctxt);
5277 if (ret) {
5278 mlog_errno(ret);
5279
5280 if (xs->not_found) {
5281 /*
5282 * We can't allocate enough clusters for outside
5283 * storage and we have allocated xattr already,
5284 * so need to remove it.
5285 */
5286 ocfs2_xattr_bucket_remove_xs(inode, ctxt->handle, xs);
5287 }
5288 goto out;
5289 }
5290
5291set_value_outside:
5292 ret = ocfs2_xattr_bucket_set_value_outside(inode, ctxt->handle,
5293 xs, val, value_len);
5294out:
5295 return ret;
5296}
5297
5298/* 5506/*
5299 * check whether the xattr bucket is filled up with the same hash value. 5507 * check whether the xattr bucket is filled up with the same hash value.
5300 * If we want to insert the xattr with the same hash, return -ENOSPC. 5508 * If we want to insert the xattr with the same hash, return -ENOSPC.
@@ -5323,156 +5531,116 @@ static int ocfs2_check_xattr_bucket_collision(struct inode *inode,
5323 return 0; 5531 return 0;
5324} 5532}
5325 5533
5326static int ocfs2_xattr_set_entry_index_block(struct inode *inode, 5534/*
5327 struct ocfs2_xattr_info *xi, 5535 * Try to set the entry in the current bucket. If we fail, the caller
5328 struct ocfs2_xattr_search *xs, 5536 * will handle getting us another bucket.
5329 struct ocfs2_xattr_set_ctxt *ctxt) 5537 */
5538static int ocfs2_xattr_set_entry_bucket(struct inode *inode,
5539 struct ocfs2_xattr_info *xi,
5540 struct ocfs2_xattr_search *xs,
5541 struct ocfs2_xattr_set_ctxt *ctxt)
5330{ 5542{
5331 struct ocfs2_xattr_header *xh; 5543 int ret;
5332 struct ocfs2_xattr_entry *xe; 5544 struct ocfs2_xa_loc loc;
5333 u16 count, header_size, xh_free_start;
5334 int free, max_free, need, old;
5335 size_t value_size = 0, name_len = strlen(xi->name);
5336 size_t blocksize = inode->i_sb->s_blocksize;
5337 int ret, allocation = 0;
5338
5339 mlog_entry("Set xattr %s in xattr index block\n", xi->name);
5340
5341try_again:
5342 xh = xs->header;
5343 count = le16_to_cpu(xh->xh_count);
5344 xh_free_start = le16_to_cpu(xh->xh_free_start);
5345 header_size = sizeof(struct ocfs2_xattr_header) +
5346 count * sizeof(struct ocfs2_xattr_entry);
5347 max_free = OCFS2_XATTR_BUCKET_SIZE - header_size -
5348 le16_to_cpu(xh->xh_name_value_len) - OCFS2_XATTR_HEADER_GAP;
5349
5350 mlog_bug_on_msg(header_size > blocksize, "bucket %llu has header size "
5351 "of %u which exceed block size\n",
5352 (unsigned long long)bucket_blkno(xs->bucket),
5353 header_size);
5354 5545
5355 if (xi->value && xi->value_len > OCFS2_XATTR_INLINE_SIZE) 5546 mlog_entry("Set xattr %s in xattr bucket\n", xi->xi_name);
5356 value_size = OCFS2_XATTR_ROOT_SIZE;
5357 else if (xi->value)
5358 value_size = OCFS2_XATTR_SIZE(xi->value_len);
5359 5547
5360 if (xs->not_found) 5548 ocfs2_init_xattr_bucket_xa_loc(&loc, xs->bucket,
5361 need = sizeof(struct ocfs2_xattr_entry) + 5549 xs->not_found ? NULL : xs->here);
5362 OCFS2_XATTR_SIZE(name_len) + value_size; 5550 ret = ocfs2_xa_set(&loc, xi, ctxt);
5363 else { 5551 if (!ret) {
5364 need = value_size + OCFS2_XATTR_SIZE(name_len); 5552 xs->here = loc.xl_entry;
5553 goto out;
5554 }
5555 if (ret != -ENOSPC) {
5556 mlog_errno(ret);
5557 goto out;
5558 }
5365 5559
5366 /* 5560 /* Ok, we need space. Let's try defragmenting the bucket. */
5367 * We only replace the old value if the new length is smaller 5561 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5368 * than the old one. Otherwise we will allocate new space in the 5562 xs->bucket);
5369 * bucket to store it. 5563 if (ret) {
5370 */ 5564 mlog_errno(ret);
5371 xe = xs->here; 5565 goto out;
5372 if (ocfs2_xattr_is_local(xe)) 5566 }
5373 old = OCFS2_XATTR_SIZE(le64_to_cpu(xe->xe_value_size));
5374 else
5375 old = OCFS2_XATTR_SIZE(OCFS2_XATTR_ROOT_SIZE);
5376 5567
5377 if (old >= value_size) 5568 ret = ocfs2_xa_set(&loc, xi, ctxt);
5378 need = 0; 5569 if (!ret) {
5570 xs->here = loc.xl_entry;
5571 goto out;
5379 } 5572 }
5573 if (ret != -ENOSPC)
5574 mlog_errno(ret);
5380 5575
5381 free = xh_free_start - header_size - OCFS2_XATTR_HEADER_GAP;
5382 /*
5383 * We need to make sure the new name/value pair
5384 * can exist in the same block.
5385 */
5386 if (xh_free_start % blocksize < need)
5387 free -= xh_free_start % blocksize;
5388
5389 mlog(0, "xs->not_found = %d, in xattr bucket %llu: free = %d, "
5390 "need = %d, max_free = %d, xh_free_start = %u, xh_name_value_len ="
5391 " %u\n", xs->not_found,
5392 (unsigned long long)bucket_blkno(xs->bucket),
5393 free, need, max_free, le16_to_cpu(xh->xh_free_start),
5394 le16_to_cpu(xh->xh_name_value_len));
5395
5396 if (free < need ||
5397 (xs->not_found &&
5398 count == ocfs2_xattr_max_xe_in_bucket(inode->i_sb))) {
5399 if (need <= max_free &&
5400 count < ocfs2_xattr_max_xe_in_bucket(inode->i_sb)) {
5401 /*
5402 * We can create the space by defragment. Since only the
5403 * name/value will be moved, the xe shouldn't be changed
5404 * in xs.
5405 */
5406 ret = ocfs2_defrag_xattr_bucket(inode, ctxt->handle,
5407 xs->bucket);
5408 if (ret) {
5409 mlog_errno(ret);
5410 goto out;
5411 }
5412 5576
5413 xh_free_start = le16_to_cpu(xh->xh_free_start); 5577out:
5414 free = xh_free_start - header_size 5578 mlog_exit(ret);
5415 - OCFS2_XATTR_HEADER_GAP; 5579 return ret;
5416 if (xh_free_start % blocksize < need) 5580}
5417 free -= xh_free_start % blocksize;
5418 5581
5419 if (free >= need) 5582static int ocfs2_xattr_set_entry_index_block(struct inode *inode,
5420 goto xattr_set; 5583 struct ocfs2_xattr_info *xi,
5584 struct ocfs2_xattr_search *xs,
5585 struct ocfs2_xattr_set_ctxt *ctxt)
5586{
5587 int ret;
5421 5588
5422 mlog(0, "Can't get enough space for xattr insert by " 5589 mlog_entry("Set xattr %s in xattr index block\n", xi->xi_name);
5423 "defragment. Need %u bytes, but we have %d, so "
5424 "allocate new bucket for it.\n", need, free);
5425 }
5426 5590
5427 /* 5591 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5428 * We have to add new buckets or clusters and one 5592 if (!ret)
5429 * allocation should leave us enough space for insert. 5593 goto out;
5430 */ 5594 if (ret != -ENOSPC) {
5431 BUG_ON(allocation); 5595 mlog_errno(ret);
5596 goto out;
5597 }
5432 5598
5433 /* 5599 /* Ack, need more space. Let's try to get another bucket! */
5434 * We do not allow for overlapping ranges between buckets. And
5435 * the maximum number of collisions we will allow for then is
5436 * one bucket's worth, so check it here whether we need to
5437 * add a new bucket for the insert.
5438 */
5439 ret = ocfs2_check_xattr_bucket_collision(inode,
5440 xs->bucket,
5441 xi->name);
5442 if (ret) {
5443 mlog_errno(ret);
5444 goto out;
5445 }
5446 5600
5447 ret = ocfs2_add_new_xattr_bucket(inode, 5601 /*
5448 xs->xattr_bh, 5602 * We do not allow for overlapping ranges between buckets. And
5603 * the maximum number of collisions we will allow for then is
5604 * one bucket's worth, so check it here whether we need to
5605 * add a new bucket for the insert.
5606 */
5607 ret = ocfs2_check_xattr_bucket_collision(inode,
5449 xs->bucket, 5608 xs->bucket,
5450 ctxt); 5609 xi->xi_name);
5451 if (ret) { 5610 if (ret) {
5452 mlog_errno(ret); 5611 mlog_errno(ret);
5453 goto out; 5612 goto out;
5454 } 5613 }
5455 5614
5456 /* 5615 ret = ocfs2_add_new_xattr_bucket(inode,
5457 * ocfs2_add_new_xattr_bucket() will have updated 5616 xs->xattr_bh,
5458 * xs->bucket if it moved, but it will not have updated 5617 xs->bucket,
5459 * any of the other search fields. Thus, we drop it and 5618 ctxt);
5460 * re-search. Everything should be cached, so it'll be 5619 if (ret) {
5461 * quick. 5620 mlog_errno(ret);
5462 */ 5621 goto out;
5463 ocfs2_xattr_bucket_relse(xs->bucket);
5464 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5465 xi->name_index,
5466 xi->name, xs);
5467 if (ret && ret != -ENODATA)
5468 goto out;
5469 xs->not_found = ret;
5470 allocation = 1;
5471 goto try_again;
5472 } 5622 }
5473 5623
5474xattr_set: 5624 /*
5475 ret = ocfs2_xattr_set_in_bucket(inode, xi, xs, ctxt); 5625 * ocfs2_add_new_xattr_bucket() will have updated
5626 * xs->bucket if it moved, but it will not have updated
5627 * any of the other search fields. Thus, we drop it and
5628 * re-search. Everything should be cached, so it'll be
5629 * quick.
5630 */
5631 ocfs2_xattr_bucket_relse(xs->bucket);
5632 ret = ocfs2_xattr_index_block_find(inode, xs->xattr_bh,
5633 xi->xi_name_index,
5634 xi->xi_name, xs);
5635 if (ret && ret != -ENODATA)
5636 goto out;
5637 xs->not_found = ret;
5638
5639 /* Ok, we have a new bucket, let's try again */
5640 ret = ocfs2_xattr_set_entry_bucket(inode, xi, xs, ctxt);
5641 if (ret && (ret != -ENOSPC))
5642 mlog_errno(ret);
5643
5476out: 5644out:
5477 mlog_exit(ret); 5645 mlog_exit(ret);
5478 return ret; 5646 return ret;
@@ -5684,7 +5852,7 @@ static int ocfs2_prepare_refcount_xattr(struct inode *inode,
5684 * refcount tree, and make the original extent become 3. So we will need 5852 * refcount tree, and make the original extent become 3. So we will need
5685 * 2 * cluster more extent recs at most. 5853 * 2 * cluster more extent recs at most.
5686 */ 5854 */
5687 if (!xi->value || xi->value_len <= OCFS2_XATTR_INLINE_SIZE) { 5855 if (!xi->xi_value || xi->xi_value_len <= OCFS2_XATTR_INLINE_SIZE) {
5688 5856
5689 ret = ocfs2_refcounted_xattr_delete_need(inode, 5857 ret = ocfs2_refcounted_xattr_delete_need(inode,
5690 &(*ref_tree)->rf_ci, 5858 &(*ref_tree)->rf_ci,
@@ -6066,7 +6234,7 @@ static int ocfs2_value_metas_in_xattr_header(struct super_block *sb,
6066 * to the extent block, so just calculate a maximum record num. 6234 * to the extent block, so just calculate a maximum record num.
6067 */ 6235 */
6068 if (!xv->xr_list.l_tree_depth) 6236 if (!xv->xr_list.l_tree_depth)
6069 *num_recs += xv->xr_list.l_next_free_rec; 6237 *num_recs += le16_to_cpu(xv->xr_list.l_next_free_rec);
6070 else 6238 else
6071 *num_recs += ocfs2_clusters_for_bytes(sb, 6239 *num_recs += ocfs2_clusters_for_bytes(sb,
6072 XATTR_SIZE_MAX); 6240 XATTR_SIZE_MAX);
@@ -6360,33 +6528,33 @@ static int ocfs2_create_empty_xattr_block(struct inode *inode,
6360 int indexed) 6528 int indexed)
6361{ 6529{
6362 int ret; 6530 int ret;
6363 handle_t *handle;
6364 struct ocfs2_alloc_context *meta_ac;
6365 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 6531 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6532 struct ocfs2_xattr_set_ctxt ctxt;
6366 6533
6367 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac); 6534 memset(&ctxt, 0, sizeof(ctxt));
6535 ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &ctxt.meta_ac);
6368 if (ret < 0) { 6536 if (ret < 0) {
6369 mlog_errno(ret); 6537 mlog_errno(ret);
6370 return ret; 6538 return ret;
6371 } 6539 }
6372 6540
6373 handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS); 6541 ctxt.handle = ocfs2_start_trans(osb, OCFS2_XATTR_BLOCK_CREATE_CREDITS);
6374 if (IS_ERR(handle)) { 6542 if (IS_ERR(ctxt.handle)) {
6375 ret = PTR_ERR(handle); 6543 ret = PTR_ERR(ctxt.handle);
6376 mlog_errno(ret); 6544 mlog_errno(ret);
6377 goto out; 6545 goto out;
6378 } 6546 }
6379 6547
6380 mlog(0, "create new xattr block for inode %llu, index = %d\n", 6548 mlog(0, "create new xattr block for inode %llu, index = %d\n",
6381 (unsigned long long)fe_bh->b_blocknr, indexed); 6549 (unsigned long long)fe_bh->b_blocknr, indexed);
6382 ret = ocfs2_create_xattr_block(handle, inode, fe_bh, 6550 ret = ocfs2_create_xattr_block(inode, fe_bh, &ctxt, indexed,
6383 meta_ac, ret_bh, indexed); 6551 ret_bh);
6384 if (ret) 6552 if (ret)
6385 mlog_errno(ret); 6553 mlog_errno(ret);
6386 6554
6387 ocfs2_commit_trans(osb, handle); 6555 ocfs2_commit_trans(osb, ctxt.handle);
6388out: 6556out:
6389 ocfs2_free_alloc_context(meta_ac); 6557 ocfs2_free_alloc_context(ctxt.meta_ac);
6390 return ret; 6558 return ret;
6391} 6559}
6392 6560
@@ -6978,9 +7146,9 @@ int ocfs2_init_security_and_acl(struct inode *dir,
6978 7146
6979 ret = ocfs2_init_security_get(inode, dir, &si); 7147 ret = ocfs2_init_security_get(inode, dir, &si);
6980 if (!ret) { 7148 if (!ret) {
6981 ret = ocfs2_xattr_security_set(inode, si.name, 7149 ret = ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY,
6982 si.value, si.value_len, 7150 si.name, si.value, si.value_len,
6983 XATTR_CREATE); 7151 XATTR_CREATE);
6984 if (ret) { 7152 if (ret) {
6985 mlog_errno(ret); 7153 mlog_errno(ret);
6986 goto leave; 7154 goto leave;
@@ -7008,9 +7176,9 @@ leave:
7008/* 7176/*
7009 * 'security' attributes support 7177 * 'security' attributes support
7010 */ 7178 */
7011static size_t ocfs2_xattr_security_list(struct inode *inode, char *list, 7179static size_t ocfs2_xattr_security_list(struct dentry *dentry, char *list,
7012 size_t list_size, const char *name, 7180 size_t list_size, const char *name,
7013 size_t name_len) 7181 size_t name_len, int type)
7014{ 7182{
7015 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN; 7183 const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
7016 const size_t total_len = prefix_len + name_len + 1; 7184 const size_t total_len = prefix_len + name_len + 1;
@@ -7023,23 +7191,23 @@ static size_t ocfs2_xattr_security_list(struct inode *inode, char *list,
7023 return total_len; 7191 return total_len;
7024} 7192}
7025 7193
7026static int ocfs2_xattr_security_get(struct inode *inode, const char *name, 7194static int ocfs2_xattr_security_get(struct dentry *dentry, const char *name,
7027 void *buffer, size_t size) 7195 void *buffer, size_t size, int type)
7028{ 7196{
7029 if (strcmp(name, "") == 0) 7197 if (strcmp(name, "") == 0)
7030 return -EINVAL; 7198 return -EINVAL;
7031 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_SECURITY, name, 7199 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7032 buffer, size); 7200 name, buffer, size);
7033} 7201}
7034 7202
7035static int ocfs2_xattr_security_set(struct inode *inode, const char *name, 7203static int ocfs2_xattr_security_set(struct dentry *dentry, const char *name,
7036 const void *value, size_t size, int flags) 7204 const void *value, size_t size, int flags, int type)
7037{ 7205{
7038 if (strcmp(name, "") == 0) 7206 if (strcmp(name, "") == 0)
7039 return -EINVAL; 7207 return -EINVAL;
7040 7208
7041 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_SECURITY, name, value, 7209 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_SECURITY,
7042 size, flags); 7210 name, value, size, flags);
7043} 7211}
7044 7212
7045int ocfs2_init_security_get(struct inode *inode, 7213int ocfs2_init_security_get(struct inode *inode,
@@ -7076,9 +7244,9 @@ struct xattr_handler ocfs2_xattr_security_handler = {
7076/* 7244/*
7077 * 'trusted' attributes support 7245 * 'trusted' attributes support
7078 */ 7246 */
7079static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list, 7247static size_t ocfs2_xattr_trusted_list(struct dentry *dentry, char *list,
7080 size_t list_size, const char *name, 7248 size_t list_size, const char *name,
7081 size_t name_len) 7249 size_t name_len, int type)
7082{ 7250{
7083 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN; 7251 const size_t prefix_len = XATTR_TRUSTED_PREFIX_LEN;
7084 const size_t total_len = prefix_len + name_len + 1; 7252 const size_t total_len = prefix_len + name_len + 1;
@@ -7091,23 +7259,23 @@ static size_t ocfs2_xattr_trusted_list(struct inode *inode, char *list,
7091 return total_len; 7259 return total_len;
7092} 7260}
7093 7261
7094static int ocfs2_xattr_trusted_get(struct inode *inode, const char *name, 7262static int ocfs2_xattr_trusted_get(struct dentry *dentry, const char *name,
7095 void *buffer, size_t size) 7263 void *buffer, size_t size, int type)
7096{ 7264{
7097 if (strcmp(name, "") == 0) 7265 if (strcmp(name, "") == 0)
7098 return -EINVAL; 7266 return -EINVAL;
7099 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_TRUSTED, name, 7267 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7100 buffer, size); 7268 name, buffer, size);
7101} 7269}
7102 7270
7103static int ocfs2_xattr_trusted_set(struct inode *inode, const char *name, 7271static int ocfs2_xattr_trusted_set(struct dentry *dentry, const char *name,
7104 const void *value, size_t size, int flags) 7272 const void *value, size_t size, int flags, int type)
7105{ 7273{
7106 if (strcmp(name, "") == 0) 7274 if (strcmp(name, "") == 0)
7107 return -EINVAL; 7275 return -EINVAL;
7108 7276
7109 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_TRUSTED, name, value, 7277 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_TRUSTED,
7110 size, flags); 7278 name, value, size, flags);
7111} 7279}
7112 7280
7113struct xattr_handler ocfs2_xattr_trusted_handler = { 7281struct xattr_handler ocfs2_xattr_trusted_handler = {
@@ -7120,13 +7288,13 @@ struct xattr_handler ocfs2_xattr_trusted_handler = {
7120/* 7288/*
7121 * 'user' attributes support 7289 * 'user' attributes support
7122 */ 7290 */
7123static size_t ocfs2_xattr_user_list(struct inode *inode, char *list, 7291static size_t ocfs2_xattr_user_list(struct dentry *dentry, char *list,
7124 size_t list_size, const char *name, 7292 size_t list_size, const char *name,
7125 size_t name_len) 7293 size_t name_len, int type)
7126{ 7294{
7127 const size_t prefix_len = XATTR_USER_PREFIX_LEN; 7295 const size_t prefix_len = XATTR_USER_PREFIX_LEN;
7128 const size_t total_len = prefix_len + name_len + 1; 7296 const size_t total_len = prefix_len + name_len + 1;
7129 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7297 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7130 7298
7131 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) 7299 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7132 return 0; 7300 return 0;
@@ -7139,31 +7307,31 @@ static size_t ocfs2_xattr_user_list(struct inode *inode, char *list,
7139 return total_len; 7307 return total_len;
7140} 7308}
7141 7309
7142static int ocfs2_xattr_user_get(struct inode *inode, const char *name, 7310static int ocfs2_xattr_user_get(struct dentry *dentry, const char *name,
7143 void *buffer, size_t size) 7311 void *buffer, size_t size, int type)
7144{ 7312{
7145 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7313 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7146 7314
7147 if (strcmp(name, "") == 0) 7315 if (strcmp(name, "") == 0)
7148 return -EINVAL; 7316 return -EINVAL;
7149 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) 7317 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7150 return -EOPNOTSUPP; 7318 return -EOPNOTSUPP;
7151 return ocfs2_xattr_get(inode, OCFS2_XATTR_INDEX_USER, name, 7319 return ocfs2_xattr_get(dentry->d_inode, OCFS2_XATTR_INDEX_USER, name,
7152 buffer, size); 7320 buffer, size);
7153} 7321}
7154 7322
7155static int ocfs2_xattr_user_set(struct inode *inode, const char *name, 7323static int ocfs2_xattr_user_set(struct dentry *dentry, const char *name,
7156 const void *value, size_t size, int flags) 7324 const void *value, size_t size, int flags, int type)
7157{ 7325{
7158 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 7326 struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
7159 7327
7160 if (strcmp(name, "") == 0) 7328 if (strcmp(name, "") == 0)
7161 return -EINVAL; 7329 return -EINVAL;
7162 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR) 7330 if (osb->s_mount_opt & OCFS2_MOUNT_NOUSERXATTR)
7163 return -EOPNOTSUPP; 7331 return -EOPNOTSUPP;
7164 7332
7165 return ocfs2_xattr_set(inode, OCFS2_XATTR_INDEX_USER, name, value, 7333 return ocfs2_xattr_set(dentry->d_inode, OCFS2_XATTR_INDEX_USER,
7166 size, flags); 7334 name, value, size, flags);
7167} 7335}
7168 7336
7169struct xattr_handler ocfs2_xattr_user_handler = { 7337struct xattr_handler ocfs2_xattr_user_handler = {