1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/capability.h>
21#include <linux/fs.h>
22#include <linux/xattr.h>
23#include <linux/posix_acl_xattr.h>
24#include <linux/slab.h>
25#include <linux/quotaops.h>
26#include <linux/security.h>
27#include "jfs_incore.h"
28#include "jfs_superblock.h"
29#include "jfs_dmap.h"
30#include "jfs_debug.h"
31#include "jfs_dinode.h"
32#include "jfs_extent.h"
33#include "jfs_metapage.h"
34#include "jfs_xattr.h"
35#include "jfs_acl.h"
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72struct ea_buffer {
73 int flag;
74 int max_size;
75 dxd_t new_ea;
76 struct metapage *mp;
77 struct jfs_ea_list *xattr;
78};
79
80
81
82
83#define EA_INLINE 0x0001
84#define EA_EXTENT 0x0002
85#define EA_NEW 0x0004
86#define EA_MALLOC 0x0008
87
88
89static int is_known_namespace(const char *name)
90{
91 if (strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN) &&
92 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) &&
93 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
94 strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN))
95 return false;
96
97 return true;
98}
99
100
101
102
103
104
105static int is_os2_xattr(struct jfs_ea *ea)
106{
107 return !is_known_namespace(ea->name);
108}
109
110static inline int name_size(struct jfs_ea *ea)
111{
112 if (is_os2_xattr(ea))
113 return ea->namelen + XATTR_OS2_PREFIX_LEN;
114 else
115 return ea->namelen;
116}
117
118static inline int copy_name(char *buffer, struct jfs_ea *ea)
119{
120 int len = ea->namelen;
121
122 if (is_os2_xattr(ea)) {
123 memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
124 buffer += XATTR_OS2_PREFIX_LEN;
125 len += XATTR_OS2_PREFIX_LEN;
126 }
127 memcpy(buffer, ea->name, ea->namelen);
128 buffer[ea->namelen] = 0;
129
130 return len;
131}
132
133
134static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
159 int size, dxd_t * ea)
160{
161 struct jfs_inode_info *ji = JFS_IP(ip);
162
163
164
165
166
167 if (ealist && size > sizeof (struct jfs_ea_list)) {
168 assert(size <= sizeof (ji->i_inline_ea));
169
170
171
172
173
174 if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
175 return -EPERM;
176
177 DXDsize(ea, size);
178 DXDlength(ea, 0);
179 DXDaddress(ea, 0);
180 memcpy(ji->i_inline_ea, ealist, size);
181 ea->flag = DXD_INLINE;
182 ji->mode2 &= ~INLINEEA;
183 } else {
184 ea->flag = 0;
185 DXDsize(ea, 0);
186 DXDlength(ea, 0);
187 DXDaddress(ea, 0);
188
189
190 if (ji->ea.flag & DXD_INLINE)
191 ji->mode2 |= INLINEEA;
192 }
193
194 return 0;
195}
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
217 dxd_t * ea)
218{
219 struct super_block *sb = ip->i_sb;
220 struct jfs_inode_info *ji = JFS_IP(ip);
221 struct jfs_sb_info *sbi = JFS_SBI(sb);
222 int nblocks;
223 s64 blkno;
224 int rc = 0, i;
225 char *cp;
226 s32 nbytes, nb;
227 s32 bytes_to_write;
228 struct metapage *mp;
229
230
231
232
233
234 if (!ealist || size <= sizeof (ji->i_inline_ea)) {
235 if (!ea_write_inline(ip, ealist, size, ea))
236 return 0;
237 }
238
239
240 nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
241
242
243 rc = dquot_alloc_block(ip, nblocks);
244 if (rc)
245 return rc;
246
247 rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
248 if (rc) {
249
250 dquot_free_block(ip, nblocks);
251 return rc;
252 }
253
254
255
256
257
258
259 cp = (char *) ealist;
260 nbytes = size;
261 for (i = 0; i < nblocks; i += sbi->nbperpage) {
262
263
264
265
266 nb = min(PSIZE, nbytes);
267 bytes_to_write =
268 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
269 << sb->s_blocksize_bits;
270
271 if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
272 rc = -EIO;
273 goto failed;
274 }
275
276 memcpy(mp->data, cp, nb);
277
278
279
280
281
282
283
284#ifdef _JFS_FIXME
285 if ((rc = flush_metapage(mp))) {
286
287
288
289
290
291
292 goto failed;
293 }
294#else
295 flush_metapage(mp);
296#endif
297
298 cp += PSIZE;
299 nbytes -= nb;
300 }
301
302 ea->flag = DXD_EXTENT;
303 DXDsize(ea, le32_to_cpu(ealist->size));
304 DXDlength(ea, nblocks);
305 DXDaddress(ea, blkno);
306
307
308 if (ji->ea.flag & DXD_INLINE)
309 ji->mode2 |= INLINEEA;
310
311 return 0;
312
313 failed:
314
315 dquot_free_block(ip, nblocks);
316
317 dbFree(ip, blkno, nblocks);
318 return rc;
319}
320
321
322
323
324
325
326
327
328
329
330
331
332static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
333{
334 struct jfs_inode_info *ji = JFS_IP(ip);
335 int ea_size = sizeDXD(&ji->ea);
336
337 if (ea_size == 0) {
338 ealist->size = 0;
339 return 0;
340 }
341
342
343 if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
344 return -EIO;
345 if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
346 != ea_size)
347 return -EIO;
348
349 memcpy(ealist, ji->i_inline_ea, ea_size);
350 return 0;
351}
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
367{
368 struct super_block *sb = ip->i_sb;
369 struct jfs_inode_info *ji = JFS_IP(ip);
370 struct jfs_sb_info *sbi = JFS_SBI(sb);
371 int nblocks;
372 s64 blkno;
373 char *cp = (char *) ealist;
374 int i;
375 int nbytes, nb;
376 s32 bytes_to_read;
377 struct metapage *mp;
378
379
380 if (ji->ea.flag & DXD_INLINE)
381 return ea_read_inline(ip, ealist);
382
383 nbytes = sizeDXD(&ji->ea);
384 if (!nbytes) {
385 jfs_error(sb, "ea_read: nbytes is 0");
386 return -EIO;
387 }
388
389
390
391
392
393 nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
394 blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
395
396
397
398
399
400
401 for (i = 0; i < nblocks; i += sbi->nbperpage) {
402
403
404
405
406 nb = min(PSIZE, nbytes);
407 bytes_to_read =
408 ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
409 << sb->s_blocksize_bits;
410
411 if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
412 return -EIO;
413
414 memcpy(cp, mp->data, nb);
415 release_metapage(mp);
416
417 cp += PSIZE;
418 nbytes -= nb;
419 }
420
421 return 0;
422}
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
442{
443 struct jfs_inode_info *ji = JFS_IP(inode);
444 struct super_block *sb = inode->i_sb;
445 int size;
446 int ea_size = sizeDXD(&ji->ea);
447 int blocks_needed, current_blocks;
448 s64 blkno;
449 int rc;
450 int quota_allocation = 0;
451
452
453 if (ji->ea.flag == 0)
454 ea_size = 0;
455
456 if (ea_size == 0) {
457 if (min_size == 0) {
458 ea_buf->flag = 0;
459 ea_buf->max_size = 0;
460 ea_buf->xattr = NULL;
461 return 0;
462 }
463 if ((min_size <= sizeof (ji->i_inline_ea)) &&
464 (ji->mode2 & INLINEEA)) {
465 ea_buf->flag = EA_INLINE | EA_NEW;
466 ea_buf->max_size = sizeof (ji->i_inline_ea);
467 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
468 DXDlength(&ea_buf->new_ea, 0);
469 DXDaddress(&ea_buf->new_ea, 0);
470 ea_buf->new_ea.flag = DXD_INLINE;
471 DXDsize(&ea_buf->new_ea, min_size);
472 return 0;
473 }
474 current_blocks = 0;
475 } else if (ji->ea.flag & DXD_INLINE) {
476 if (min_size <= sizeof (ji->i_inline_ea)) {
477 ea_buf->flag = EA_INLINE;
478 ea_buf->max_size = sizeof (ji->i_inline_ea);
479 ea_buf->xattr = (struct jfs_ea_list *) ji->i_inline_ea;
480 goto size_check;
481 }
482 current_blocks = 0;
483 } else {
484 if (!(ji->ea.flag & DXD_EXTENT)) {
485 jfs_error(sb, "ea_get: invalid ea.flag)");
486 return -EIO;
487 }
488 current_blocks = (ea_size + sb->s_blocksize - 1) >>
489 sb->s_blocksize_bits;
490 }
491 size = max(min_size, ea_size);
492
493 if (size > PSIZE) {
494
495
496
497
498 ea_buf->xattr = kmalloc(size, GFP_KERNEL);
499 if (ea_buf->xattr == NULL)
500 return -ENOMEM;
501
502 ea_buf->flag = EA_MALLOC;
503 ea_buf->max_size = (size + sb->s_blocksize - 1) &
504 ~(sb->s_blocksize - 1);
505
506 if (ea_size == 0)
507 return 0;
508
509 if ((rc = ea_read(inode, ea_buf->xattr))) {
510 kfree(ea_buf->xattr);
511 ea_buf->xattr = NULL;
512 return rc;
513 }
514 goto size_check;
515 }
516 blocks_needed = (min_size + sb->s_blocksize - 1) >>
517 sb->s_blocksize_bits;
518
519 if (blocks_needed > current_blocks) {
520
521 rc = dquot_alloc_block(inode, blocks_needed);
522 if (rc)
523 return -EDQUOT;
524
525 quota_allocation = blocks_needed;
526
527 rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
528 &blkno);
529 if (rc)
530 goto clean_up;
531
532 DXDlength(&ea_buf->new_ea, blocks_needed);
533 DXDaddress(&ea_buf->new_ea, blkno);
534 ea_buf->new_ea.flag = DXD_EXTENT;
535 DXDsize(&ea_buf->new_ea, min_size);
536
537 ea_buf->flag = EA_EXTENT | EA_NEW;
538
539 ea_buf->mp = get_metapage(inode, blkno,
540 blocks_needed << sb->s_blocksize_bits,
541 1);
542 if (ea_buf->mp == NULL) {
543 dbFree(inode, blkno, (s64) blocks_needed);
544 rc = -EIO;
545 goto clean_up;
546 }
547 ea_buf->xattr = ea_buf->mp->data;
548 ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
549 ~(sb->s_blocksize - 1);
550 if (ea_size == 0)
551 return 0;
552 if ((rc = ea_read(inode, ea_buf->xattr))) {
553 discard_metapage(ea_buf->mp);
554 dbFree(inode, blkno, (s64) blocks_needed);
555 goto clean_up;
556 }
557 goto size_check;
558 }
559 ea_buf->flag = EA_EXTENT;
560 ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
561 lengthDXD(&ji->ea) << sb->s_blocksize_bits,
562 1);
563 if (ea_buf->mp == NULL) {
564 rc = -EIO;
565 goto clean_up;
566 }
567 ea_buf->xattr = ea_buf->mp->data;
568 ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
569 ~(sb->s_blocksize - 1);
570
571 size_check:
572 if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
573 printk(KERN_ERR "ea_get: invalid extended attribute\n");
574 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1,
575 ea_buf->xattr, ea_size, 1);
576 ea_release(inode, ea_buf);
577 rc = -EIO;
578 goto clean_up;
579 }
580
581 return ea_size;
582
583 clean_up:
584
585 if (quota_allocation)
586 dquot_free_block(inode, quota_allocation);
587
588 return (rc);
589}
590
591static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
592{
593 if (ea_buf->flag & EA_MALLOC)
594 kfree(ea_buf->xattr);
595 else if (ea_buf->flag & EA_EXTENT) {
596 assert(ea_buf->mp);
597 release_metapage(ea_buf->mp);
598
599 if (ea_buf->flag & EA_NEW)
600 dbFree(inode, addressDXD(&ea_buf->new_ea),
601 lengthDXD(&ea_buf->new_ea));
602 }
603}
604
605static int ea_put(tid_t tid, struct inode *inode, struct ea_buffer *ea_buf,
606 int new_size)
607{
608 struct jfs_inode_info *ji = JFS_IP(inode);
609 unsigned long old_blocks, new_blocks;
610 int rc = 0;
611
612 if (new_size == 0) {
613 ea_release(inode, ea_buf);
614 ea_buf = NULL;
615 } else if (ea_buf->flag & EA_INLINE) {
616 assert(new_size <= sizeof (ji->i_inline_ea));
617 ji->mode2 &= ~INLINEEA;
618 ea_buf->new_ea.flag = DXD_INLINE;
619 DXDsize(&ea_buf->new_ea, new_size);
620 DXDaddress(&ea_buf->new_ea, 0);
621 DXDlength(&ea_buf->new_ea, 0);
622 } else if (ea_buf->flag & EA_MALLOC) {
623 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
624 kfree(ea_buf->xattr);
625 } else if (ea_buf->flag & EA_NEW) {
626
627 flush_metapage(ea_buf->mp);
628 } else {
629
630 rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
631 discard_metapage(ea_buf->mp);
632 }
633 if (rc)
634 return rc;
635
636 old_blocks = new_blocks = 0;
637
638 if (ji->ea.flag & DXD_EXTENT) {
639 invalidate_dxd_metapages(inode, ji->ea);
640 old_blocks = lengthDXD(&ji->ea);
641 }
642
643 if (ea_buf) {
644 txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
645 if (ea_buf->new_ea.flag & DXD_EXTENT) {
646 new_blocks = lengthDXD(&ea_buf->new_ea);
647 if (ji->ea.flag & DXD_INLINE)
648 ji->mode2 |= INLINEEA;
649 }
650 ji->ea = ea_buf->new_ea;
651 } else {
652 txEA(tid, inode, &ji->ea, NULL);
653 if (ji->ea.flag & DXD_INLINE)
654 ji->mode2 |= INLINEEA;
655 ji->ea.flag = 0;
656 ji->ea.size = 0;
657 }
658
659
660 if (old_blocks)
661 dquot_free_block(inode, old_blocks);
662
663 inode->i_ctime = CURRENT_TIME;
664
665 return 0;
666}
667
668
669
670
671
672
673
674static int can_set_system_xattr(struct inode *inode, const char *name,
675 const void *value, size_t value_len)
676{
677#ifdef CONFIG_JFS_POSIX_ACL
678 struct posix_acl *acl;
679 int rc;
680
681 if (!is_owner_or_cap(inode))
682 return -EPERM;
683
684
685
686
687 if (strcmp(name, POSIX_ACL_XATTR_ACCESS) == 0) {
688 acl = posix_acl_from_xattr(value, value_len);
689 if (IS_ERR(acl)) {
690 rc = PTR_ERR(acl);
691 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
692 rc);
693 return rc;
694 }
695 if (acl) {
696 mode_t mode = inode->i_mode;
697 rc = posix_acl_equiv_mode(acl, &mode);
698 posix_acl_release(acl);
699 if (rc < 0) {
700 printk(KERN_ERR
701 "posix_acl_equiv_mode returned %d\n",
702 rc);
703 return rc;
704 }
705 inode->i_mode = mode;
706 mark_inode_dirty(inode);
707 }
708
709
710
711 forget_cached_acl(inode, ACL_TYPE_ACCESS);
712
713 return 0;
714 } else if (strcmp(name, POSIX_ACL_XATTR_DEFAULT) == 0) {
715 acl = posix_acl_from_xattr(value, value_len);
716 if (IS_ERR(acl)) {
717 rc = PTR_ERR(acl);
718 printk(KERN_ERR "posix_acl_from_xattr returned %d\n",
719 rc);
720 return rc;
721 }
722 posix_acl_release(acl);
723
724
725
726
727 forget_cached_acl(inode, ACL_TYPE_DEFAULT);
728
729 return 0;
730 }
731#endif
732 return -EOPNOTSUPP;
733}
734
735
736
737
738
739
740static int can_set_xattr(struct inode *inode, const char *name,
741 const void *value, size_t value_len)
742{
743 if (!strncmp(name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
744 return can_set_system_xattr(inode, name, value, value_len);
745
746 if (!strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN)) {
747
748
749
750
751
752 if (is_known_namespace(name + XATTR_OS2_PREFIX_LEN))
753 return -EOPNOTSUPP;
754 return 0;
755 }
756
757
758
759
760 if (strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) &&
761 strncmp(name, XATTR_SECURITY_PREFIX, XATTR_SECURITY_PREFIX_LEN) &&
762 strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
763 return -EOPNOTSUPP;
764
765 return 0;
766}
767
768int __jfs_setxattr(tid_t tid, struct inode *inode, const char *name,
769 const void *value, size_t value_len, int flags)
770{
771 struct jfs_ea_list *ealist;
772 struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
773 struct ea_buffer ea_buf;
774 int old_ea_size = 0;
775 int xattr_size;
776 int new_size;
777 int namelen = strlen(name);
778 char *os2name = NULL;
779 int found = 0;
780 int rc;
781 int length;
782
783 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
784 os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
785 GFP_KERNEL);
786 if (!os2name)
787 return -ENOMEM;
788 strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
789 name = os2name;
790 namelen -= XATTR_OS2_PREFIX_LEN;
791 }
792
793 down_write(&JFS_IP(inode)->xattr_sem);
794
795 xattr_size = ea_get(inode, &ea_buf, 0);
796 if (xattr_size < 0) {
797 rc = xattr_size;
798 goto out;
799 }
800
801 again:
802 ealist = (struct jfs_ea_list *) ea_buf.xattr;
803 new_size = sizeof (struct jfs_ea_list);
804
805 if (xattr_size) {
806 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
807 ea = NEXT_EA(ea)) {
808 if ((namelen == ea->namelen) &&
809 (memcmp(name, ea->name, namelen) == 0)) {
810 found = 1;
811 if (flags & XATTR_CREATE) {
812 rc = -EEXIST;
813 goto release;
814 }
815 old_ea = ea;
816 old_ea_size = EA_SIZE(ea);
817 next_ea = NEXT_EA(ea);
818 } else
819 new_size += EA_SIZE(ea);
820 }
821 }
822
823 if (!found) {
824 if (flags & XATTR_REPLACE) {
825 rc = -ENODATA;
826 goto release;
827 }
828 if (value == NULL) {
829 rc = 0;
830 goto release;
831 }
832 }
833 if (value)
834 new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
835
836 if (new_size > ea_buf.max_size) {
837
838
839
840
841 ea_release(inode, &ea_buf);
842 xattr_size = ea_get(inode, &ea_buf, new_size);
843 if (xattr_size < 0) {
844 rc = xattr_size;
845 goto out;
846 }
847 goto again;
848 }
849
850
851 if (found) {
852
853 length = (char *) END_EALIST(ealist) - (char *) next_ea;
854 if (length > 0)
855 memmove(old_ea, next_ea, length);
856 xattr_size -= old_ea_size;
857 }
858
859
860 if (value) {
861 if (xattr_size == 0)
862
863 xattr_size = sizeof (struct jfs_ea_list);
864
865 ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
866 ea->flag = 0;
867 ea->namelen = namelen;
868 ea->valuelen = (cpu_to_le16(value_len));
869 memcpy(ea->name, name, namelen);
870 ea->name[namelen] = 0;
871 if (value_len)
872 memcpy(&ea->name[namelen + 1], value, value_len);
873 xattr_size += EA_SIZE(ea);
874 }
875
876
877 if (xattr_size != new_size) {
878 printk(KERN_ERR
879 "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
880 xattr_size, new_size);
881
882 rc = -EINVAL;
883 goto release;
884 }
885
886
887
888
889 if (new_size == sizeof (struct jfs_ea_list))
890 new_size = 0;
891
892 ealist->size = cpu_to_le32(new_size);
893
894 rc = ea_put(tid, inode, &ea_buf, new_size);
895
896 goto out;
897 release:
898 ea_release(inode, &ea_buf);
899 out:
900 up_write(&JFS_IP(inode)->xattr_sem);
901
902 kfree(os2name);
903
904 return rc;
905}
906
907int jfs_setxattr(struct dentry *dentry, const char *name, const void *value,
908 size_t value_len, int flags)
909{
910 struct inode *inode = dentry->d_inode;
911 struct jfs_inode_info *ji = JFS_IP(inode);
912 int rc;
913 tid_t tid;
914
915 if ((rc = can_set_xattr(inode, name, value, value_len)))
916 return rc;
917
918 if (value == NULL) {
919 value = "";
920 value_len = 0;
921 }
922
923 tid = txBegin(inode->i_sb, 0);
924 mutex_lock(&ji->commit_mutex);
925 rc = __jfs_setxattr(tid, dentry->d_inode, name, value, value_len,
926 flags);
927 if (!rc)
928 rc = txCommit(tid, 1, &inode, 0);
929 txEnd(tid);
930 mutex_unlock(&ji->commit_mutex);
931
932 return rc;
933}
934
935ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
936 size_t buf_size)
937{
938 struct jfs_ea_list *ealist;
939 struct jfs_ea *ea;
940 struct ea_buffer ea_buf;
941 int xattr_size;
942 ssize_t size;
943 int namelen = strlen(name);
944 char *value;
945
946 down_read(&JFS_IP(inode)->xattr_sem);
947
948 xattr_size = ea_get(inode, &ea_buf, 0);
949
950 if (xattr_size < 0) {
951 size = xattr_size;
952 goto out;
953 }
954
955 if (xattr_size == 0)
956 goto not_found;
957
958 ealist = (struct jfs_ea_list *) ea_buf.xattr;
959
960
961 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
962 if ((namelen == ea->namelen) &&
963 memcmp(name, ea->name, namelen) == 0) {
964
965 size = le16_to_cpu(ea->valuelen);
966 if (!data)
967 goto release;
968 else if (size > buf_size) {
969 size = -ERANGE;
970 goto release;
971 }
972 value = ((char *) &ea->name) + ea->namelen + 1;
973 memcpy(data, value, size);
974 goto release;
975 }
976 not_found:
977 size = -ENODATA;
978 release:
979 ea_release(inode, &ea_buf);
980 out:
981 up_read(&JFS_IP(inode)->xattr_sem);
982
983 return size;
984}
985
986ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
987 size_t buf_size)
988{
989 int err;
990
991 if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
992
993
994
995 name += XATTR_OS2_PREFIX_LEN;
996
997
998
999
1000 if (is_known_namespace(name))
1001 return -EOPNOTSUPP;
1002 }
1003
1004 err = __jfs_getxattr(dentry->d_inode, name, data, buf_size);
1005
1006 return err;
1007}
1008
1009
1010
1011
1012static inline int can_list(struct jfs_ea *ea)
1013{
1014 return (strncmp(ea->name, XATTR_TRUSTED_PREFIX,
1015 XATTR_TRUSTED_PREFIX_LEN) ||
1016 capable(CAP_SYS_ADMIN));
1017}
1018
1019ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
1020{
1021 struct inode *inode = dentry->d_inode;
1022 char *buffer;
1023 ssize_t size = 0;
1024 int xattr_size;
1025 struct jfs_ea_list *ealist;
1026 struct jfs_ea *ea;
1027 struct ea_buffer ea_buf;
1028
1029 down_read(&JFS_IP(inode)->xattr_sem);
1030
1031 xattr_size = ea_get(inode, &ea_buf, 0);
1032 if (xattr_size < 0) {
1033 size = xattr_size;
1034 goto out;
1035 }
1036
1037 if (xattr_size == 0)
1038 goto release;
1039
1040 ealist = (struct jfs_ea_list *) ea_buf.xattr;
1041
1042
1043 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1044 if (can_list(ea))
1045 size += name_size(ea) + 1;
1046 }
1047
1048 if (!data)
1049 goto release;
1050
1051 if (size > buf_size) {
1052 size = -ERANGE;
1053 goto release;
1054 }
1055
1056
1057 buffer = data;
1058 for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
1059 if (can_list(ea)) {
1060 int namelen = copy_name(buffer, ea);
1061 buffer += namelen + 1;
1062 }
1063 }
1064
1065 release:
1066 ea_release(inode, &ea_buf);
1067 out:
1068 up_read(&JFS_IP(inode)->xattr_sem);
1069 return size;
1070}
1071
1072int jfs_removexattr(struct dentry *dentry, const char *name)
1073{
1074 struct inode *inode = dentry->d_inode;
1075 struct jfs_inode_info *ji = JFS_IP(inode);
1076 int rc;
1077 tid_t tid;
1078
1079 if ((rc = can_set_xattr(inode, name, NULL, 0)))
1080 return rc;
1081
1082 tid = txBegin(inode->i_sb, 0);
1083 mutex_lock(&ji->commit_mutex);
1084 rc = __jfs_setxattr(tid, dentry->d_inode, name, NULL, 0, XATTR_REPLACE);
1085 if (!rc)
1086 rc = txCommit(tid, 1, &inode, 0);
1087 txEnd(tid);
1088 mutex_unlock(&ji->commit_mutex);
1089
1090 return rc;
1091}
1092
1093#ifdef CONFIG_JFS_SECURITY
1094int jfs_init_security(tid_t tid, struct inode *inode, struct inode *dir)
1095{
1096 int rc;
1097 size_t len;
1098 void *value;
1099 char *suffix;
1100 char *name;
1101
1102 rc = security_inode_init_security(inode, dir, &suffix, &value, &len);
1103 if (rc) {
1104 if (rc == -EOPNOTSUPP)
1105 return 0;
1106 return rc;
1107 }
1108 name = kmalloc(XATTR_SECURITY_PREFIX_LEN + 1 + strlen(suffix),
1109 GFP_NOFS);
1110 if (!name) {
1111 rc = -ENOMEM;
1112 goto kmalloc_failed;
1113 }
1114 strcpy(name, XATTR_SECURITY_PREFIX);
1115 strcpy(name + XATTR_SECURITY_PREFIX_LEN, suffix);
1116
1117 rc = __jfs_setxattr(tid, inode, name, value, len, 0);
1118
1119 kfree(name);
1120kmalloc_failed:
1121 kfree(suffix);
1122 kfree(value);
1123
1124 return rc;
1125}
1126#endif
1127