1
2
3
4
5
6
7
8
9
10#include <linux/slab.h>
11#include <linux/spinlock.h>
12#include <linux/completion.h>
13#include <linux/buffer_head.h>
14#include <linux/xattr.h>
15#include <linux/gfs2_ondisk.h>
16#include <asm/uaccess.h>
17
18#include "gfs2.h"
19#include "incore.h"
20#include "acl.h"
21#include "xattr.h"
22#include "glock.h"
23#include "inode.h"
24#include "meta_io.h"
25#include "quota.h"
26#include "rgrp.h"
27#include "trans.h"
28#include "util.h"
29
30
31
32
33
34
35
36
37
38
39
40static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize,
41 unsigned int *size)
42{
43 unsigned int jbsize = sdp->sd_jbsize;
44
45
46 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8);
47
48 if (*size <= jbsize)
49 return 1;
50
51
52 *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize +
53 (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8);
54
55 return 0;
56}
57
58static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize)
59{
60 unsigned int size;
61
62 if (dsize > GFS2_EA_MAX_DATA_LEN)
63 return -ERANGE;
64
65 ea_calc_size(sdp, nsize, dsize, &size);
66
67
68 if (size > sdp->sd_jbsize)
69 return -ERANGE;
70
71 return 0;
72}
73
74typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh,
75 struct gfs2_ea_header *ea,
76 struct gfs2_ea_header *prev, void *private);
77
78static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh,
79 ea_call_t ea_call, void *data)
80{
81 struct gfs2_ea_header *ea, *prev = NULL;
82 int error = 0;
83
84 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA))
85 return -EIO;
86
87 for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) {
88 if (!GFS2_EA_REC_LEN(ea))
89 goto fail;
90 if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <=
91 bh->b_data + bh->b_size))
92 goto fail;
93 if (!GFS2_EATYPE_VALID(ea->ea_type))
94 goto fail;
95
96 error = ea_call(ip, bh, ea, prev, data);
97 if (error)
98 return error;
99
100 if (GFS2_EA_IS_LAST(ea)) {
101 if ((char *)GFS2_EA2NEXT(ea) !=
102 bh->b_data + bh->b_size)
103 goto fail;
104 break;
105 }
106 }
107
108 return error;
109
110fail:
111 gfs2_consist_inode(ip);
112 return -EIO;
113}
114
115static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data)
116{
117 struct buffer_head *bh, *eabh;
118 __be64 *eablk, *end;
119 int error;
120
121 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &bh);
122 if (error)
123 return error;
124
125 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) {
126 error = ea_foreach_i(ip, bh, ea_call, data);
127 goto out;
128 }
129
130 if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) {
131 error = -EIO;
132 goto out;
133 }
134
135 eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header));
136 end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs;
137
138 for (; eablk < end; eablk++) {
139 u64 bn;
140
141 if (!*eablk)
142 break;
143 bn = be64_to_cpu(*eablk);
144
145 error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, 0, &eabh);
146 if (error)
147 break;
148 error = ea_foreach_i(ip, eabh, ea_call, data);
149 brelse(eabh);
150 if (error)
151 break;
152 }
153out:
154 brelse(bh);
155 return error;
156}
157
158struct ea_find {
159 int type;
160 const char *name;
161 size_t namel;
162 struct gfs2_ea_location *ef_el;
163};
164
165static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh,
166 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
167 void *private)
168{
169 struct ea_find *ef = private;
170
171 if (ea->ea_type == GFS2_EATYPE_UNUSED)
172 return 0;
173
174 if (ea->ea_type == ef->type) {
175 if (ea->ea_name_len == ef->namel &&
176 !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) {
177 struct gfs2_ea_location *el = ef->ef_el;
178 get_bh(bh);
179 el->el_bh = bh;
180 el->el_ea = ea;
181 el->el_prev = prev;
182 return 1;
183 }
184 }
185
186 return 0;
187}
188
189static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name,
190 struct gfs2_ea_location *el)
191{
192 struct ea_find ef;
193 int error;
194
195 ef.type = type;
196 ef.name = name;
197 ef.namel = strlen(name);
198 ef.ef_el = el;
199
200 memset(el, 0, sizeof(struct gfs2_ea_location));
201
202 error = ea_foreach(ip, ea_find_i, &ef);
203 if (error > 0)
204 return 0;
205
206 return error;
207}
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
225 struct gfs2_ea_header *ea,
226 struct gfs2_ea_header *prev, void *private)
227{
228 int *leave = private;
229 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
230 struct gfs2_rgrpd *rgd;
231 struct gfs2_holder rg_gh;
232 __be64 *dataptrs;
233 u64 bn = 0;
234 u64 bstart = 0;
235 unsigned int blen = 0;
236 unsigned int blks = 0;
237 unsigned int x;
238 int error;
239
240 error = gfs2_rindex_update(sdp);
241 if (error)
242 return error;
243
244 if (GFS2_EA_IS_STUFFED(ea))
245 return 0;
246
247 dataptrs = GFS2_EA2DATAPTRS(ea);
248 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
249 if (*dataptrs) {
250 blks++;
251 bn = be64_to_cpu(*dataptrs);
252 }
253 }
254 if (!blks)
255 return 0;
256
257 rgd = gfs2_blk2rgrpd(sdp, bn, 1);
258 if (!rgd) {
259 gfs2_consist_inode(ip);
260 return -EIO;
261 }
262
263 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh);
264 if (error)
265 return error;
266
267 error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE +
268 RES_EATTR + RES_STATFS + RES_QUOTA, blks);
269 if (error)
270 goto out_gunlock;
271
272 gfs2_trans_add_meta(ip->i_gl, bh);
273
274 dataptrs = GFS2_EA2DATAPTRS(ea);
275 for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) {
276 if (!*dataptrs)
277 break;
278 bn = be64_to_cpu(*dataptrs);
279
280 if (bstart + blen == bn)
281 blen++;
282 else {
283 if (bstart)
284 gfs2_free_meta(ip, bstart, blen);
285 bstart = bn;
286 blen = 1;
287 }
288
289 *dataptrs = 0;
290 gfs2_add_inode_blocks(&ip->i_inode, -1);
291 }
292 if (bstart)
293 gfs2_free_meta(ip, bstart, blen);
294
295 if (prev && !leave) {
296 u32 len;
297
298 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
299 prev->ea_rec_len = cpu_to_be32(len);
300
301 if (GFS2_EA_IS_LAST(ea))
302 prev->ea_flags |= GFS2_EAFLAG_LAST;
303 } else {
304 ea->ea_type = GFS2_EATYPE_UNUSED;
305 ea->ea_num_ptrs = 0;
306 }
307
308 ip->i_inode.i_ctime = CURRENT_TIME;
309 __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
310
311 gfs2_trans_end(sdp);
312
313out_gunlock:
314 gfs2_glock_dq_uninit(&rg_gh);
315 return error;
316}
317
318static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh,
319 struct gfs2_ea_header *ea,
320 struct gfs2_ea_header *prev, int leave)
321{
322 int error;
323
324 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
325 if (error)
326 return error;
327
328 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
329 if (error)
330 goto out_alloc;
331
332 error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL);
333
334 gfs2_quota_unhold(ip);
335out_alloc:
336 return error;
337}
338
339struct ea_list {
340 struct gfs2_ea_request *ei_er;
341 unsigned int ei_size;
342};
343
344static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea)
345{
346 switch (ea->ea_type) {
347 case GFS2_EATYPE_USR:
348 return 5 + ea->ea_name_len + 1;
349 case GFS2_EATYPE_SYS:
350 return 7 + ea->ea_name_len + 1;
351 case GFS2_EATYPE_SECURITY:
352 return 9 + ea->ea_name_len + 1;
353 default:
354 return 0;
355 }
356}
357
358static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh,
359 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
360 void *private)
361{
362 struct ea_list *ei = private;
363 struct gfs2_ea_request *er = ei->ei_er;
364 unsigned int ea_size = gfs2_ea_strlen(ea);
365
366 if (ea->ea_type == GFS2_EATYPE_UNUSED)
367 return 0;
368
369 if (er->er_data_len) {
370 char *prefix = NULL;
371 unsigned int l = 0;
372 char c = 0;
373
374 if (ei->ei_size + ea_size > er->er_data_len)
375 return -ERANGE;
376
377 switch (ea->ea_type) {
378 case GFS2_EATYPE_USR:
379 prefix = "user.";
380 l = 5;
381 break;
382 case GFS2_EATYPE_SYS:
383 prefix = "system.";
384 l = 7;
385 break;
386 case GFS2_EATYPE_SECURITY:
387 prefix = "security.";
388 l = 9;
389 break;
390 }
391
392 BUG_ON(l == 0);
393
394 memcpy(er->er_data + ei->ei_size, prefix, l);
395 memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea),
396 ea->ea_name_len);
397 memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1);
398 }
399
400 ei->ei_size += ea_size;
401
402 return 0;
403}
404
405
406
407
408
409
410
411
412
413
414ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size)
415{
416 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
417 struct gfs2_ea_request er;
418 struct gfs2_holder i_gh;
419 int error;
420
421 memset(&er, 0, sizeof(struct gfs2_ea_request));
422 if (size) {
423 er.er_data = buffer;
424 er.er_data_len = size;
425 }
426
427 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
428 if (error)
429 return error;
430
431 if (ip->i_eattr) {
432 struct ea_list ei = { .ei_er = &er, .ei_size = 0 };
433
434 error = ea_foreach(ip, ea_list_i, &ei);
435 if (!error)
436 error = ei.ei_size;
437 }
438
439 gfs2_glock_dq_uninit(&i_gh);
440
441 return error;
442}
443
444
445
446
447
448
449
450
451
452
453
454
455static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
456 const char *din, char *dout)
457{
458 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
459 struct buffer_head **bh;
460 unsigned int amount = GFS2_EA_DATA_LEN(ea);
461 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
462 __be64 *dataptrs = GFS2_EA2DATAPTRS(ea);
463 unsigned int x;
464 int error = 0;
465 unsigned char *pos;
466 unsigned cp_size;
467
468 bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS);
469 if (!bh)
470 return -ENOMEM;
471
472 for (x = 0; x < nptrs; x++) {
473 error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0,
474 bh + x);
475 if (error) {
476 while (x--)
477 brelse(bh[x]);
478 goto out;
479 }
480 dataptrs++;
481 }
482
483 for (x = 0; x < nptrs; x++) {
484 error = gfs2_meta_wait(sdp, bh[x]);
485 if (error) {
486 for (; x < nptrs; x++)
487 brelse(bh[x]);
488 goto out;
489 }
490 if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) {
491 for (; x < nptrs; x++)
492 brelse(bh[x]);
493 error = -EIO;
494 goto out;
495 }
496
497 pos = bh[x]->b_data + sizeof(struct gfs2_meta_header);
498 cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize;
499
500 if (dout) {
501 memcpy(dout, pos, cp_size);
502 dout += sdp->sd_jbsize;
503 }
504
505 if (din) {
506 gfs2_trans_add_meta(ip->i_gl, bh[x]);
507 memcpy(pos, din, cp_size);
508 din += sdp->sd_jbsize;
509 }
510
511 amount -= sdp->sd_jbsize;
512 brelse(bh[x]);
513 }
514
515out:
516 kfree(bh);
517 return error;
518}
519
520static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el,
521 char *data, size_t size)
522{
523 int ret;
524 size_t len = GFS2_EA_DATA_LEN(el->el_ea);
525 if (len > size)
526 return -ERANGE;
527
528 if (GFS2_EA_IS_STUFFED(el->el_ea)) {
529 memcpy(data, GFS2_EA2DATA(el->el_ea), len);
530 return len;
531 }
532 ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data);
533 if (ret < 0)
534 return ret;
535 return len;
536}
537
538int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata)
539{
540 struct gfs2_ea_location el;
541 int error;
542 int len;
543 char *data;
544
545 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el);
546 if (error)
547 return error;
548 if (!el.el_ea)
549 goto out;
550 if (!GFS2_EA_DATA_LEN(el.el_ea))
551 goto out;
552
553 len = GFS2_EA_DATA_LEN(el.el_ea);
554 data = kmalloc(len, GFP_NOFS);
555 error = -ENOMEM;
556 if (data == NULL)
557 goto out;
558
559 error = gfs2_ea_get_copy(ip, &el, data, len);
560 if (error < 0)
561 kfree(data);
562 else
563 *ppdata = data;
564out:
565 brelse(el.el_bh);
566 return error;
567}
568
569
570
571
572
573
574
575
576
577
578
579static int gfs2_xattr_get(struct dentry *dentry, const char *name,
580 void *buffer, size_t size, int type)
581{
582 struct gfs2_inode *ip = GFS2_I(dentry->d_inode);
583 struct gfs2_ea_location el;
584 int error;
585
586 if (!ip->i_eattr)
587 return -ENODATA;
588 if (strlen(name) > GFS2_EA_MAX_NAME_LEN)
589 return -EINVAL;
590
591 error = gfs2_ea_find(ip, type, name, &el);
592 if (error)
593 return error;
594 if (!el.el_ea)
595 return -ENODATA;
596 if (size)
597 error = gfs2_ea_get_copy(ip, &el, buffer, size);
598 else
599 error = GFS2_EA_DATA_LEN(el.el_ea);
600 brelse(el.el_bh);
601
602 return error;
603}
604
605
606
607
608
609
610
611
612
613static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp)
614{
615 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
616 struct gfs2_ea_header *ea;
617 unsigned int n = 1;
618 u64 block;
619 int error;
620
621 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
622 if (error)
623 return error;
624 gfs2_trans_add_unrevoke(sdp, block, 1);
625 *bhp = gfs2_meta_new(ip->i_gl, block);
626 gfs2_trans_add_meta(ip->i_gl, *bhp);
627 gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA);
628 gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header));
629
630 ea = GFS2_EA_BH2FIRST(*bhp);
631 ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize);
632 ea->ea_type = GFS2_EATYPE_UNUSED;
633 ea->ea_flags = GFS2_EAFLAG_LAST;
634 ea->ea_num_ptrs = 0;
635
636 gfs2_add_inode_blocks(&ip->i_inode, 1);
637
638 return 0;
639}
640
641
642
643
644
645
646
647
648
649
650
651
652
653static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea,
654 struct gfs2_ea_request *er)
655{
656 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
657 int error;
658
659 ea->ea_data_len = cpu_to_be32(er->er_data_len);
660 ea->ea_name_len = er->er_name_len;
661 ea->ea_type = er->er_type;
662 ea->__pad = 0;
663
664 memcpy(GFS2_EA2NAME(ea), er->er_name, er->er_name_len);
665
666 if (GFS2_EAREQ_SIZE_STUFFED(er) <= sdp->sd_jbsize) {
667 ea->ea_num_ptrs = 0;
668 memcpy(GFS2_EA2DATA(ea), er->er_data, er->er_data_len);
669 } else {
670 __be64 *dataptr = GFS2_EA2DATAPTRS(ea);
671 const char *data = er->er_data;
672 unsigned int data_len = er->er_data_len;
673 unsigned int copy;
674 unsigned int x;
675
676 ea->ea_num_ptrs = DIV_ROUND_UP(er->er_data_len, sdp->sd_jbsize);
677 for (x = 0; x < ea->ea_num_ptrs; x++) {
678 struct buffer_head *bh;
679 u64 block;
680 int mh_size = sizeof(struct gfs2_meta_header);
681 unsigned int n = 1;
682
683 error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL);
684 if (error)
685 return error;
686 gfs2_trans_add_unrevoke(sdp, block, 1);
687 bh = gfs2_meta_new(ip->i_gl, block);
688 gfs2_trans_add_meta(ip->i_gl, bh);
689 gfs2_metatype_set(bh, GFS2_METATYPE_ED, GFS2_FORMAT_ED);
690
691 gfs2_add_inode_blocks(&ip->i_inode, 1);
692
693 copy = data_len > sdp->sd_jbsize ? sdp->sd_jbsize :
694 data_len;
695 memcpy(bh->b_data + mh_size, data, copy);
696 if (copy < sdp->sd_jbsize)
697 memset(bh->b_data + mh_size + copy, 0,
698 sdp->sd_jbsize - copy);
699
700 *dataptr++ = cpu_to_be64(bh->b_blocknr);
701 data += copy;
702 data_len -= copy;
703
704 brelse(bh);
705 }
706
707 gfs2_assert_withdraw(sdp, !data_len);
708 }
709
710 return 0;
711}
712
713typedef int (*ea_skeleton_call_t) (struct gfs2_inode *ip,
714 struct gfs2_ea_request *er, void *private);
715
716static int ea_alloc_skeleton(struct gfs2_inode *ip, struct gfs2_ea_request *er,
717 unsigned int blks,
718 ea_skeleton_call_t skeleton_call, void *private)
719{
720 struct gfs2_alloc_parms ap = { .target = blks };
721 int error;
722
723 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
724 if (error)
725 return error;
726
727 error = gfs2_quota_lock_check(ip, &ap);
728 if (error)
729 return error;
730
731 error = gfs2_inplace_reserve(ip, &ap);
732 if (error)
733 goto out_gunlock_q;
734
735 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode),
736 blks + gfs2_rg_blocks(ip, blks) +
737 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
738 if (error)
739 goto out_ipres;
740
741 error = skeleton_call(ip, er, private);
742 if (error)
743 goto out_end_trans;
744
745 ip->i_inode.i_ctime = CURRENT_TIME;
746 __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
747
748out_end_trans:
749 gfs2_trans_end(GFS2_SB(&ip->i_inode));
750out_ipres:
751 gfs2_inplace_release(ip);
752out_gunlock_q:
753 gfs2_quota_unlock(ip);
754 return error;
755}
756
757static int ea_init_i(struct gfs2_inode *ip, struct gfs2_ea_request *er,
758 void *private)
759{
760 struct buffer_head *bh;
761 int error;
762
763 error = ea_alloc_blk(ip, &bh);
764 if (error)
765 return error;
766
767 ip->i_eattr = bh->b_blocknr;
768 error = ea_write(ip, GFS2_EA_BH2FIRST(bh), er);
769
770 brelse(bh);
771
772 return error;
773}
774
775
776
777
778
779
780
781
782
783static int ea_init(struct gfs2_inode *ip, int type, const char *name,
784 const void *data, size_t size)
785{
786 struct gfs2_ea_request er;
787 unsigned int jbsize = GFS2_SB(&ip->i_inode)->sd_jbsize;
788 unsigned int blks = 1;
789
790 er.er_type = type;
791 er.er_name = name;
792 er.er_name_len = strlen(name);
793 er.er_data = (void *)data;
794 er.er_data_len = size;
795
796 if (GFS2_EAREQ_SIZE_STUFFED(&er) > jbsize)
797 blks += DIV_ROUND_UP(er.er_data_len, jbsize);
798
799 return ea_alloc_skeleton(ip, &er, blks, ea_init_i, NULL);
800}
801
802static struct gfs2_ea_header *ea_split_ea(struct gfs2_ea_header *ea)
803{
804 u32 ea_size = GFS2_EA_SIZE(ea);
805 struct gfs2_ea_header *new = (struct gfs2_ea_header *)((char *)ea +
806 ea_size);
807 u32 new_size = GFS2_EA_REC_LEN(ea) - ea_size;
808 int last = ea->ea_flags & GFS2_EAFLAG_LAST;
809
810 ea->ea_rec_len = cpu_to_be32(ea_size);
811 ea->ea_flags ^= last;
812
813 new->ea_rec_len = cpu_to_be32(new_size);
814 new->ea_flags = last;
815
816 return new;
817}
818
819static void ea_set_remove_stuffed(struct gfs2_inode *ip,
820 struct gfs2_ea_location *el)
821{
822 struct gfs2_ea_header *ea = el->el_ea;
823 struct gfs2_ea_header *prev = el->el_prev;
824 u32 len;
825
826 gfs2_trans_add_meta(ip->i_gl, el->el_bh);
827
828 if (!prev || !GFS2_EA_IS_STUFFED(ea)) {
829 ea->ea_type = GFS2_EATYPE_UNUSED;
830 return;
831 } else if (GFS2_EA2NEXT(prev) != ea) {
832 prev = GFS2_EA2NEXT(prev);
833 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode), GFS2_EA2NEXT(prev) == ea);
834 }
835
836 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
837 prev->ea_rec_len = cpu_to_be32(len);
838
839 if (GFS2_EA_IS_LAST(ea))
840 prev->ea_flags |= GFS2_EAFLAG_LAST;
841}
842
843struct ea_set {
844 int ea_split;
845
846 struct gfs2_ea_request *es_er;
847 struct gfs2_ea_location *es_el;
848
849 struct buffer_head *es_bh;
850 struct gfs2_ea_header *es_ea;
851};
852
853static int ea_set_simple_noalloc(struct gfs2_inode *ip, struct buffer_head *bh,
854 struct gfs2_ea_header *ea, struct ea_set *es)
855{
856 struct gfs2_ea_request *er = es->es_er;
857 int error;
858
859 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + 2 * RES_EATTR, 0);
860 if (error)
861 return error;
862
863 gfs2_trans_add_meta(ip->i_gl, bh);
864
865 if (es->ea_split)
866 ea = ea_split_ea(ea);
867
868 ea_write(ip, ea, er);
869
870 if (es->es_el)
871 ea_set_remove_stuffed(ip, es->es_el);
872
873 ip->i_inode.i_ctime = CURRENT_TIME;
874 __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
875
876 gfs2_trans_end(GFS2_SB(&ip->i_inode));
877 return error;
878}
879
880static int ea_set_simple_alloc(struct gfs2_inode *ip,
881 struct gfs2_ea_request *er, void *private)
882{
883 struct ea_set *es = private;
884 struct gfs2_ea_header *ea = es->es_ea;
885 int error;
886
887 gfs2_trans_add_meta(ip->i_gl, es->es_bh);
888
889 if (es->ea_split)
890 ea = ea_split_ea(ea);
891
892 error = ea_write(ip, ea, er);
893 if (error)
894 return error;
895
896 if (es->es_el)
897 ea_set_remove_stuffed(ip, es->es_el);
898
899 return 0;
900}
901
902static int ea_set_simple(struct gfs2_inode *ip, struct buffer_head *bh,
903 struct gfs2_ea_header *ea, struct gfs2_ea_header *prev,
904 void *private)
905{
906 struct ea_set *es = private;
907 unsigned int size;
908 int stuffed;
909 int error;
910
911 stuffed = ea_calc_size(GFS2_SB(&ip->i_inode), es->es_er->er_name_len,
912 es->es_er->er_data_len, &size);
913
914 if (ea->ea_type == GFS2_EATYPE_UNUSED) {
915 if (GFS2_EA_REC_LEN(ea) < size)
916 return 0;
917 if (!GFS2_EA_IS_STUFFED(ea)) {
918 error = ea_remove_unstuffed(ip, bh, ea, prev, 1);
919 if (error)
920 return error;
921 }
922 es->ea_split = 0;
923 } else if (GFS2_EA_REC_LEN(ea) - GFS2_EA_SIZE(ea) >= size)
924 es->ea_split = 1;
925 else
926 return 0;
927
928 if (stuffed) {
929 error = ea_set_simple_noalloc(ip, bh, ea, es);
930 if (error)
931 return error;
932 } else {
933 unsigned int blks;
934
935 es->es_bh = bh;
936 es->es_ea = ea;
937 blks = 2 + DIV_ROUND_UP(es->es_er->er_data_len,
938 GFS2_SB(&ip->i_inode)->sd_jbsize);
939
940 error = ea_alloc_skeleton(ip, es->es_er, blks,
941 ea_set_simple_alloc, es);
942 if (error)
943 return error;
944 }
945
946 return 1;
947}
948
949static int ea_set_block(struct gfs2_inode *ip, struct gfs2_ea_request *er,
950 void *private)
951{
952 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
953 struct buffer_head *indbh, *newbh;
954 __be64 *eablk;
955 int error;
956 int mh_size = sizeof(struct gfs2_meta_header);
957
958 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
959 __be64 *end;
960
961 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0,
962 &indbh);
963 if (error)
964 return error;
965
966 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
967 error = -EIO;
968 goto out;
969 }
970
971 eablk = (__be64 *)(indbh->b_data + mh_size);
972 end = eablk + sdp->sd_inptrs;
973
974 for (; eablk < end; eablk++)
975 if (!*eablk)
976 break;
977
978 if (eablk == end) {
979 error = -ENOSPC;
980 goto out;
981 }
982
983 gfs2_trans_add_meta(ip->i_gl, indbh);
984 } else {
985 u64 blk;
986 unsigned int n = 1;
987 error = gfs2_alloc_blocks(ip, &blk, &n, 0, NULL);
988 if (error)
989 return error;
990 gfs2_trans_add_unrevoke(sdp, blk, 1);
991 indbh = gfs2_meta_new(ip->i_gl, blk);
992 gfs2_trans_add_meta(ip->i_gl, indbh);
993 gfs2_metatype_set(indbh, GFS2_METATYPE_IN, GFS2_FORMAT_IN);
994 gfs2_buffer_clear_tail(indbh, mh_size);
995
996 eablk = (__be64 *)(indbh->b_data + mh_size);
997 *eablk = cpu_to_be64(ip->i_eattr);
998 ip->i_eattr = blk;
999 ip->i_diskflags |= GFS2_DIF_EA_INDIRECT;
1000 gfs2_add_inode_blocks(&ip->i_inode, 1);
1001
1002 eablk++;
1003 }
1004
1005 error = ea_alloc_blk(ip, &newbh);
1006 if (error)
1007 goto out;
1008
1009 *eablk = cpu_to_be64((u64)newbh->b_blocknr);
1010 error = ea_write(ip, GFS2_EA_BH2FIRST(newbh), er);
1011 brelse(newbh);
1012 if (error)
1013 goto out;
1014
1015 if (private)
1016 ea_set_remove_stuffed(ip, private);
1017
1018out:
1019 brelse(indbh);
1020 return error;
1021}
1022
1023static int ea_set_i(struct gfs2_inode *ip, int type, const char *name,
1024 const void *value, size_t size, struct gfs2_ea_location *el)
1025{
1026 struct gfs2_ea_request er;
1027 struct ea_set es;
1028 unsigned int blks = 2;
1029 int error;
1030
1031 er.er_type = type;
1032 er.er_name = name;
1033 er.er_data = (void *)value;
1034 er.er_name_len = strlen(name);
1035 er.er_data_len = size;
1036
1037 memset(&es, 0, sizeof(struct ea_set));
1038 es.es_er = &er;
1039 es.es_el = el;
1040
1041 error = ea_foreach(ip, ea_set_simple, &es);
1042 if (error > 0)
1043 return 0;
1044 if (error)
1045 return error;
1046
1047 if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT))
1048 blks++;
1049 if (GFS2_EAREQ_SIZE_STUFFED(&er) > GFS2_SB(&ip->i_inode)->sd_jbsize)
1050 blks += DIV_ROUND_UP(er.er_data_len, GFS2_SB(&ip->i_inode)->sd_jbsize);
1051
1052 return ea_alloc_skeleton(ip, &er, blks, ea_set_block, el);
1053}
1054
1055static int ea_set_remove_unstuffed(struct gfs2_inode *ip,
1056 struct gfs2_ea_location *el)
1057{
1058 if (el->el_prev && GFS2_EA2NEXT(el->el_prev) != el->el_ea) {
1059 el->el_prev = GFS2_EA2NEXT(el->el_prev);
1060 gfs2_assert_withdraw(GFS2_SB(&ip->i_inode),
1061 GFS2_EA2NEXT(el->el_prev) == el->el_ea);
1062 }
1063
1064 return ea_remove_unstuffed(ip, el->el_bh, el->el_ea, el->el_prev, 0);
1065}
1066
1067static int ea_remove_stuffed(struct gfs2_inode *ip, struct gfs2_ea_location *el)
1068{
1069 struct gfs2_ea_header *ea = el->el_ea;
1070 struct gfs2_ea_header *prev = el->el_prev;
1071 int error;
1072
1073 error = gfs2_trans_begin(GFS2_SB(&ip->i_inode), RES_DINODE + RES_EATTR, 0);
1074 if (error)
1075 return error;
1076
1077 gfs2_trans_add_meta(ip->i_gl, el->el_bh);
1078
1079 if (prev) {
1080 u32 len;
1081
1082 len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea);
1083 prev->ea_rec_len = cpu_to_be32(len);
1084
1085 if (GFS2_EA_IS_LAST(ea))
1086 prev->ea_flags |= GFS2_EAFLAG_LAST;
1087 } else {
1088 ea->ea_type = GFS2_EATYPE_UNUSED;
1089 }
1090
1091 ip->i_inode.i_ctime = CURRENT_TIME;
1092 __mark_inode_dirty(&ip->i_inode, I_DIRTY_SYNC | I_DIRTY_DATASYNC);
1093
1094 gfs2_trans_end(GFS2_SB(&ip->i_inode));
1095
1096 return error;
1097}
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112static int gfs2_xattr_remove(struct gfs2_inode *ip, int type, const char *name)
1113{
1114 struct gfs2_ea_location el;
1115 int error;
1116
1117 if (!ip->i_eattr)
1118 return -ENODATA;
1119
1120 error = gfs2_ea_find(ip, type, name, &el);
1121 if (error)
1122 return error;
1123 if (!el.el_ea)
1124 return -ENODATA;
1125
1126 if (GFS2_EA_IS_STUFFED(el.el_ea))
1127 error = ea_remove_stuffed(ip, &el);
1128 else
1129 error = ea_remove_unstuffed(ip, el.el_bh, el.el_ea, el.el_prev, 0);
1130
1131 brelse(el.el_bh);
1132
1133 return error;
1134}
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150int __gfs2_xattr_set(struct inode *inode, const char *name,
1151 const void *value, size_t size, int flags, int type)
1152{
1153 struct gfs2_inode *ip = GFS2_I(inode);
1154 struct gfs2_sbd *sdp = GFS2_SB(inode);
1155 struct gfs2_ea_location el;
1156 unsigned int namel = strlen(name);
1157 int error;
1158
1159 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
1160 return -EPERM;
1161 if (namel > GFS2_EA_MAX_NAME_LEN)
1162 return -ERANGE;
1163
1164 if (value == NULL)
1165 return gfs2_xattr_remove(ip, type, name);
1166
1167 if (ea_check_size(sdp, namel, size))
1168 return -ERANGE;
1169
1170 if (!ip->i_eattr) {
1171 if (flags & XATTR_REPLACE)
1172 return -ENODATA;
1173 return ea_init(ip, type, name, value, size);
1174 }
1175
1176 error = gfs2_ea_find(ip, type, name, &el);
1177 if (error)
1178 return error;
1179
1180 if (el.el_ea) {
1181 if (ip->i_diskflags & GFS2_DIF_APPENDONLY) {
1182 brelse(el.el_bh);
1183 return -EPERM;
1184 }
1185
1186 error = -EEXIST;
1187 if (!(flags & XATTR_CREATE)) {
1188 int unstuffed = !GFS2_EA_IS_STUFFED(el.el_ea);
1189 error = ea_set_i(ip, type, name, value, size, &el);
1190 if (!error && unstuffed)
1191 ea_set_remove_unstuffed(ip, &el);
1192 }
1193
1194 brelse(el.el_bh);
1195 return error;
1196 }
1197
1198 error = -ENODATA;
1199 if (!(flags & XATTR_REPLACE))
1200 error = ea_set_i(ip, type, name, value, size, NULL);
1201
1202 return error;
1203}
1204
1205static int gfs2_xattr_set(struct dentry *dentry, const char *name,
1206 const void *value, size_t size, int flags, int type)
1207{
1208 return __gfs2_xattr_set(dentry->d_inode, name, value,
1209 size, flags, type);
1210}
1211
1212
1213static int ea_acl_chmod_unstuffed(struct gfs2_inode *ip,
1214 struct gfs2_ea_header *ea, char *data)
1215{
1216 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1217 unsigned int amount = GFS2_EA_DATA_LEN(ea);
1218 unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize);
1219 int ret;
1220
1221 ret = gfs2_trans_begin(sdp, nptrs + RES_DINODE, 0);
1222 if (ret)
1223 return ret;
1224
1225 ret = gfs2_iter_unstuffed(ip, ea, data, NULL);
1226 gfs2_trans_end(sdp);
1227
1228 return ret;
1229}
1230
1231int gfs2_xattr_acl_chmod(struct gfs2_inode *ip, struct iattr *attr, char *data)
1232{
1233 struct inode *inode = &ip->i_inode;
1234 struct gfs2_sbd *sdp = GFS2_SB(inode);
1235 struct gfs2_ea_location el;
1236 int error;
1237
1238 error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, GFS2_POSIX_ACL_ACCESS, &el);
1239 if (error)
1240 return error;
1241
1242 if (GFS2_EA_IS_STUFFED(el.el_ea)) {
1243 error = gfs2_trans_begin(sdp, RES_DINODE + RES_EATTR, 0);
1244 if (error == 0) {
1245 gfs2_trans_add_meta(ip->i_gl, el.el_bh);
1246 memcpy(GFS2_EA2DATA(el.el_ea), data,
1247 GFS2_EA_DATA_LEN(el.el_ea));
1248 }
1249 } else {
1250 error = ea_acl_chmod_unstuffed(ip, el.el_ea, data);
1251 }
1252
1253 brelse(el.el_bh);
1254 if (error)
1255 return error;
1256
1257 error = gfs2_setattr_simple(inode, attr);
1258 gfs2_trans_end(sdp);
1259 return error;
1260}
1261
1262static int ea_dealloc_indirect(struct gfs2_inode *ip)
1263{
1264 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1265 struct gfs2_rgrp_list rlist;
1266 struct buffer_head *indbh, *dibh;
1267 __be64 *eablk, *end;
1268 unsigned int rg_blocks = 0;
1269 u64 bstart = 0;
1270 unsigned int blen = 0;
1271 unsigned int blks = 0;
1272 unsigned int x;
1273 int error;
1274
1275 error = gfs2_rindex_update(sdp);
1276 if (error)
1277 return error;
1278
1279 memset(&rlist, 0, sizeof(struct gfs2_rgrp_list));
1280
1281 error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &indbh);
1282 if (error)
1283 return error;
1284
1285 if (gfs2_metatype_check(sdp, indbh, GFS2_METATYPE_IN)) {
1286 error = -EIO;
1287 goto out;
1288 }
1289
1290 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1291 end = eablk + sdp->sd_inptrs;
1292
1293 for (; eablk < end; eablk++) {
1294 u64 bn;
1295
1296 if (!*eablk)
1297 break;
1298 bn = be64_to_cpu(*eablk);
1299
1300 if (bstart + blen == bn)
1301 blen++;
1302 else {
1303 if (bstart)
1304 gfs2_rlist_add(ip, &rlist, bstart);
1305 bstart = bn;
1306 blen = 1;
1307 }
1308 blks++;
1309 }
1310 if (bstart)
1311 gfs2_rlist_add(ip, &rlist, bstart);
1312 else
1313 goto out;
1314
1315 gfs2_rlist_alloc(&rlist, LM_ST_EXCLUSIVE);
1316
1317 for (x = 0; x < rlist.rl_rgrps; x++) {
1318 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(rlist.rl_ghs[x].gh_gl);
1319
1320 rg_blocks += rgd->rd_length;
1321 }
1322
1323 error = gfs2_glock_nq_m(rlist.rl_rgrps, rlist.rl_ghs);
1324 if (error)
1325 goto out_rlist_free;
1326
1327 error = gfs2_trans_begin(sdp, rg_blocks + RES_DINODE + RES_INDIRECT +
1328 RES_STATFS + RES_QUOTA, blks);
1329 if (error)
1330 goto out_gunlock;
1331
1332 gfs2_trans_add_meta(ip->i_gl, indbh);
1333
1334 eablk = (__be64 *)(indbh->b_data + sizeof(struct gfs2_meta_header));
1335 bstart = 0;
1336 blen = 0;
1337
1338 for (; eablk < end; eablk++) {
1339 u64 bn;
1340
1341 if (!*eablk)
1342 break;
1343 bn = be64_to_cpu(*eablk);
1344
1345 if (bstart + blen == bn)
1346 blen++;
1347 else {
1348 if (bstart)
1349 gfs2_free_meta(ip, bstart, blen);
1350 bstart = bn;
1351 blen = 1;
1352 }
1353
1354 *eablk = 0;
1355 gfs2_add_inode_blocks(&ip->i_inode, -1);
1356 }
1357 if (bstart)
1358 gfs2_free_meta(ip, bstart, blen);
1359
1360 ip->i_diskflags &= ~GFS2_DIF_EA_INDIRECT;
1361
1362 error = gfs2_meta_inode_buffer(ip, &dibh);
1363 if (!error) {
1364 gfs2_trans_add_meta(ip->i_gl, dibh);
1365 gfs2_dinode_out(ip, dibh->b_data);
1366 brelse(dibh);
1367 }
1368
1369 gfs2_trans_end(sdp);
1370
1371out_gunlock:
1372 gfs2_glock_dq_m(rlist.rl_rgrps, rlist.rl_ghs);
1373out_rlist_free:
1374 gfs2_rlist_free(&rlist);
1375out:
1376 brelse(indbh);
1377 return error;
1378}
1379
1380static int ea_dealloc_block(struct gfs2_inode *ip)
1381{
1382 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
1383 struct gfs2_rgrpd *rgd;
1384 struct buffer_head *dibh;
1385 struct gfs2_holder gh;
1386 int error;
1387
1388 error = gfs2_rindex_update(sdp);
1389 if (error)
1390 return error;
1391
1392 rgd = gfs2_blk2rgrpd(sdp, ip->i_eattr, 1);
1393 if (!rgd) {
1394 gfs2_consist_inode(ip);
1395 return -EIO;
1396 }
1397
1398 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
1399 if (error)
1400 return error;
1401
1402 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_DINODE + RES_STATFS +
1403 RES_QUOTA, 1);
1404 if (error)
1405 goto out_gunlock;
1406
1407 gfs2_free_meta(ip, ip->i_eattr, 1);
1408
1409 ip->i_eattr = 0;
1410 gfs2_add_inode_blocks(&ip->i_inode, -1);
1411
1412 error = gfs2_meta_inode_buffer(ip, &dibh);
1413 if (!error) {
1414 gfs2_trans_add_meta(ip->i_gl, dibh);
1415 gfs2_dinode_out(ip, dibh->b_data);
1416 brelse(dibh);
1417 }
1418
1419 gfs2_trans_end(sdp);
1420
1421out_gunlock:
1422 gfs2_glock_dq_uninit(&gh);
1423 return error;
1424}
1425
1426
1427
1428
1429
1430
1431
1432
1433int gfs2_ea_dealloc(struct gfs2_inode *ip)
1434{
1435 int error;
1436
1437 error = gfs2_rindex_update(GFS2_SB(&ip->i_inode));
1438 if (error)
1439 return error;
1440
1441 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
1442 if (error)
1443 return error;
1444
1445 error = ea_foreach(ip, ea_dealloc_unstuffed, NULL);
1446 if (error)
1447 goto out_quota;
1448
1449 if (ip->i_diskflags & GFS2_DIF_EA_INDIRECT) {
1450 error = ea_dealloc_indirect(ip);
1451 if (error)
1452 goto out_quota;
1453 }
1454
1455 error = ea_dealloc_block(ip);
1456
1457out_quota:
1458 gfs2_quota_unhold(ip);
1459 return error;
1460}
1461
1462static const struct xattr_handler gfs2_xattr_user_handler = {
1463 .prefix = XATTR_USER_PREFIX,
1464 .flags = GFS2_EATYPE_USR,
1465 .get = gfs2_xattr_get,
1466 .set = gfs2_xattr_set,
1467};
1468
1469static const struct xattr_handler gfs2_xattr_security_handler = {
1470 .prefix = XATTR_SECURITY_PREFIX,
1471 .flags = GFS2_EATYPE_SECURITY,
1472 .get = gfs2_xattr_get,
1473 .set = gfs2_xattr_set,
1474};
1475
1476const struct xattr_handler *gfs2_xattr_handlers[] = {
1477 &gfs2_xattr_user_handler,
1478 &gfs2_xattr_security_handler,
1479 &gfs2_xattr_system_handler,
1480 NULL,
1481};
1482
1483