OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [fs/] [jfs/] [xattr.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *   Copyright (C) International Business Machines  Corp., 2000-2003
3
 *   Copyright (C) Christoph Hellwig, 2002
4
 *
5
 *   This program is free software;  you can redistribute it and/or modify
6
 *   it under the terms of the GNU General Public License as published by
7
 *   the Free Software Foundation; either version 2 of the License, or
8
 *   (at your option) any later version.
9
 *
10
 *   This program is distributed in the hope that it will be useful,
11
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13
 *   the GNU General Public License for more details.
14
 *
15
 *   You should have received a copy of the GNU General Public License
16
 *   along with this program;  if not, write to the Free Software
17
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
 */
19
 
20
#include <linux/fs.h>
21
#include <linux/xattr.h>
22
#include "jfs_incore.h"
23
#include "jfs_superblock.h"
24
#include "jfs_dmap.h"
25
#include "jfs_debug.h"
26
#include "jfs_dinode.h"
27
#include "jfs_extent.h"
28
#include "jfs_metapage.h"
29
#include "jfs_xattr.h"
30
 
31
/*
32
 *      jfs_xattr.c: extended attribute service
33
 *
34
 * Overall design --
35
 *
36
 * Format:
37
 *
38
 *   Extended attribute lists (jfs_ea_list) consist of an overall size (32 bit
39
 *   value) and a variable (0 or more) number of extended attribute
40
 *   entries.  Each extended attribute entry (jfs_ea) is a <name,value> double
41
 *   where <name> is constructed from a null-terminated ascii string
42
 *   (1 ... 255 bytes in the name) and <value> is arbitrary 8 bit data
43
 *   (1 ... 65535 bytes).  The in-memory format is
44
 *
45
 *   0       1        2        4                4 + namelen + 1
46
 *   +-------+--------+--------+----------------+-------------------+
47
 *   | Flags | Name   | Value  | Name String \0 | Data . . . .      |
48
 *   |       | Length | Length |                |                   |
49
 *   +-------+--------+--------+----------------+-------------------+
50
 *
51
 *   A jfs_ea_list then is structured as
52
 *
53
 *   0            4                   4 + EA_SIZE(ea1)
54
 *   +------------+-------------------+--------------------+-----
55
 *   | Overall EA | First FEA Element | Second FEA Element | .....
56
 *   | List Size  |                   |                    |
57
 *   +------------+-------------------+--------------------+-----
58
 *
59
 *   On-disk:
60
 *
61
 *     FEALISTs are stored on disk using blocks allocated by dbAlloc() and
62
 *     written directly. An EA list may be in-lined in the inode if there is
63
 *     sufficient room available.
64
 */
65
 
66
struct ea_buffer {
67
        int flag;               /* Indicates what storage xattr points to */
68
        int max_size;           /* largest xattr that fits in current buffer */
69
        dxd_t new_ea;           /* dxd to replace ea when modifying xattr */
70
        struct metapage *mp;    /* metapage containing ea list */
71
        struct jfs_ea_list *xattr;      /* buffer containing ea list */
72
};
73
 
74
/*
75
 * ea_buffer.flag values
76
 */
77
#define EA_INLINE       0x0001
78
#define EA_EXTENT       0x0002
79
#define EA_NEW          0x0004
80
#define EA_MALLOC       0x0008
81
 
82
/* Namespaces */
83
#define XATTR_SYSTEM_PREFIX "system."
84
#define XATTR_SYSTEM_PREFIX_LEN (sizeof (XATTR_SYSTEM_PREFIX) - 1)
85
 
86
#define XATTR_USER_PREFIX "user."
87
#define XATTR_USER_PREFIX_LEN (sizeof (XATTR_USER_PREFIX) - 1)
88
 
89
#define XATTR_OS2_PREFIX "os2."
90
#define XATTR_OS2_PREFIX_LEN (sizeof (XATTR_OS2_PREFIX) - 1)
91
 
92
/*
93
 * These three routines are used to recognize on-disk extended attributes
94
 * that are in a recognized namespace.  If the attribute is not recognized,
95
 * "os2." is prepended to the name
96
 */
97
static inline int is_os2_xattr(struct jfs_ea *ea)
98
{
99
        /*
100
         * Check for "system."
101
         */
102
        if ((ea->namelen >= XATTR_SYSTEM_PREFIX_LEN) &&
103
            !strncmp(ea->name, XATTR_SYSTEM_PREFIX, XATTR_SYSTEM_PREFIX_LEN))
104
                return FALSE;
105
        /*
106
         * Check for "user."
107
         */
108
        if ((ea->namelen >= XATTR_USER_PREFIX_LEN) &&
109
            !strncmp(ea->name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN))
110
                return FALSE;
111
        /*
112
         * Add any other valid namespace prefixes here
113
         */
114
 
115
        /*
116
         * We assume it's OS/2's flat namespace
117
         */
118
        return TRUE;
119
}
120
 
121
static inline int name_size(struct jfs_ea *ea)
122
{
123
        if (is_os2_xattr(ea))
124
                return ea->namelen + XATTR_OS2_PREFIX_LEN;
125
        else
126
                return ea->namelen;
127
}
128
 
129
static inline int copy_name(char *buffer, struct jfs_ea *ea)
130
{
131
        int len = ea->namelen;
132
 
133
        if (is_os2_xattr(ea)) {
134
                memcpy(buffer, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN);
135
                buffer += XATTR_OS2_PREFIX_LEN;
136
                len += XATTR_OS2_PREFIX_LEN;
137
        }
138
        memcpy(buffer, ea->name, ea->namelen);
139
        buffer[ea->namelen] = 0;
140
 
141
        return len;
142
}
143
 
144
/* Forward references */
145
static void ea_release(struct inode *inode, struct ea_buffer *ea_buf);
146
 
147
/*
148
 * NAME: ea_write_inline
149
 *
150
 * FUNCTION: Attempt to write an EA inline if area is available
151
 *
152
 * PRE CONDITIONS:
153
 *      Already verified that the specified EA is small enough to fit inline
154
 *
155
 * PARAMETERS:
156
 *      ip      - Inode pointer
157
 *      ealist  - EA list pointer
158
 *      size    - size of ealist in bytes
159
 *      ea      - dxd_t structure to be filled in with necessary EA information
160
 *                if we successfully copy the EA inline
161
 *
162
 * NOTES:
163
 *      Checks if the inode's inline area is available.  If so, copies EA inline
164
 *      and sets <ea> fields appropriately.  Otherwise, returns failure, EA will
165
 *      have to be put into an extent.
166
 *
167
 * RETURNS: 0 for successful copy to inline area; -1 if area not available
168
 */
169
static int ea_write_inline(struct inode *ip, struct jfs_ea_list *ealist,
170
                           int size, dxd_t * ea)
171
{
172
        struct jfs_inode_info *ji = JFS_IP(ip);
173
 
174
        /*
175
         * Make sure we have an EA -- the NULL EA list is valid, but you
176
         * can't copy it!
177
         */
178
        if (ealist && size > sizeof (struct jfs_ea_list)) {
179
                assert(size <= sizeof (ji->i_inline_ea));
180
 
181
                /*
182
                 * See if the space is available or if it is already being
183
                 * used for an inline EA.
184
                 */
185
                if (!(ji->mode2 & INLINEEA) && !(ji->ea.flag & DXD_INLINE))
186
                        return -EPERM;
187
 
188
                DXDsize(ea, size);
189
                DXDlength(ea, 0);
190
                DXDaddress(ea, 0);
191
                memcpy(ji->i_inline_ea, ealist, size);
192
                ea->flag = DXD_INLINE;
193
                ji->mode2 &= ~INLINEEA;
194
        } else {
195
                ea->flag = 0;
196
                DXDsize(ea, 0);
197
                DXDlength(ea, 0);
198
                DXDaddress(ea, 0);
199
 
200
                /* Free up INLINE area */
201
                if (ji->ea.flag & DXD_INLINE)
202
                        ji->mode2 |= INLINEEA;
203
        }
204
 
205
        mark_inode_dirty(ip);
206
        return 0;
207
}
208
 
209
/*
210
 * NAME: ea_write
211
 *
212
 * FUNCTION: Write an EA for an inode
213
 *
214
 * PRE CONDITIONS: EA has been verified
215
 *
216
 * PARAMETERS:
217
 *      ip      - Inode pointer
218
 *      ealist  - EA list pointer
219
 *      size    - size of ealist in bytes
220
 *      ea      - dxd_t structure to be filled in appropriately with where the
221
 *                EA was copied
222
 *
223
 * NOTES: Will write EA inline if able to, otherwise allocates blocks for an
224
 *      extent and synchronously writes it to those blocks.
225
 *
226
 * RETURNS: 0 for success; Anything else indicates failure
227
 */
228
static int ea_write(struct inode *ip, struct jfs_ea_list *ealist, int size,
229
                       dxd_t * ea)
230
{
231
        struct super_block *sb = ip->i_sb;
232
        struct jfs_inode_info *ji = JFS_IP(ip);
233
        struct jfs_sb_info *sbi = JFS_SBI(sb);
234
        int nblocks;
235
        s64 blkno;
236
        int rc = 0, i;
237
        char *cp;
238
        s32 nbytes, nb;
239
        s32 bytes_to_write;
240
        struct metapage *mp;
241
 
242
        /*
243
         * Quick check to see if this is an in-linable EA.  Short EAs
244
         * and empty EAs are all in-linable, provided the space exists.
245
         */
246
        if (!ealist || size <= sizeof (ji->i_inline_ea)) {
247
                if (!ea_write_inline(ip, ealist, size, ea))
248
                        return 0;
249
        }
250
 
251
        /* figure out how many blocks we need */
252
        nblocks = (size + (sb->s_blocksize - 1)) >> sb->s_blocksize_bits;
253
 
254
        rc = dbAlloc(ip, INOHINT(ip), nblocks, &blkno);
255
        if (rc)
256
                return rc;
257
 
258
        /*
259
         * Now have nblocks worth of storage to stuff into the FEALIST.
260
         * loop over the FEALIST copying data into the buffer one page at
261
         * a time.
262
         */
263
        cp = (char *) ealist;
264
        nbytes = size;
265
        for (i = 0; i < nblocks; i += sbi->nbperpage) {
266
                /*
267
                 * Determine how many bytes for this request, and round up to
268
                 * the nearest aggregate block size
269
                 */
270
                nb = min(PSIZE, nbytes);
271
                bytes_to_write =
272
                    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
273
                    << sb->s_blocksize_bits;
274
 
275
                if (!(mp = get_metapage(ip, blkno + i, bytes_to_write, 1))) {
276
                        rc = -EIO;
277
                        goto failed;
278
                }
279
 
280
                memcpy(mp->data, cp, nb);
281
 
282
                /*
283
                 * We really need a way to propagate errors for
284
                 * forced writes like this one.  --hch
285
                 *
286
                 * (__write_metapage => release_metapage => flush_metapage)
287
                 */
288
#ifdef _JFS_FIXME
289
                if ((rc = flush_metapage(mp))) {
290
                        /*
291
                         * the write failed -- this means that the buffer
292
                         * is still assigned and the blocks are not being
293
                         * used.  this seems like the best error recovery
294
                         * we can get ...
295
                         */
296
                        goto failed;
297
                }
298
#else
299
                flush_metapage(mp);
300
#endif
301
 
302
                cp += PSIZE;
303
                nbytes -= nb;
304
        }
305
 
306
        ea->flag = DXD_EXTENT;
307
        DXDsize(ea, le32_to_cpu(ealist->size));
308
        DXDlength(ea, nblocks);
309
        DXDaddress(ea, blkno);
310
 
311
        /* Free up INLINE area */
312
        if (ji->ea.flag & DXD_INLINE)
313
                ji->mode2 |= INLINEEA;
314
 
315
        return 0;
316
 
317
      failed:
318
        dbFree(ip, blkno, nblocks);
319
        return rc;
320
}
321
 
322
/*
323
 * NAME: ea_read_inline
324
 *
325
 * FUNCTION: Read an inlined EA into user's buffer
326
 *
327
 * PARAMETERS:
328
 *      ip      - Inode pointer
329
 *      ealist  - Pointer to buffer to fill in with EA
330
 *
331
 * RETURNS: 0
332
 */
333
static int ea_read_inline(struct inode *ip, struct jfs_ea_list *ealist)
334
{
335
        struct jfs_inode_info *ji = JFS_IP(ip);
336
        int ea_size = sizeDXD(&ji->ea);
337
 
338
        if (ea_size == 0) {
339
                ealist->size = 0;
340
                return 0;
341
        }
342
 
343
        /* Sanity Check */
344
        if ((sizeDXD(&ji->ea) > sizeof (ji->i_inline_ea)))
345
                return -EIO;
346
        if (le32_to_cpu(((struct jfs_ea_list *) &ji->i_inline_ea)->size)
347
            != ea_size)
348
                return -EIO;
349
 
350
        memcpy(ealist, ji->i_inline_ea, ea_size);
351
        return 0;
352
}
353
 
354
/*
355
 * NAME: ea_read
356
 *
357
 * FUNCTION: copy EA data into user's buffer
358
 *
359
 * PARAMETERS:
360
 *      ip      - Inode pointer
361
 *      ealist  - Pointer to buffer to fill in with EA
362
 *
363
 * NOTES:  If EA is inline calls ea_read_inline() to copy EA.
364
 *
365
 * RETURNS: 0 for success; other indicates failure
366
 */
367
static int ea_read(struct inode *ip, struct jfs_ea_list *ealist)
368
{
369
        struct super_block *sb = ip->i_sb;
370
        struct jfs_inode_info *ji = JFS_IP(ip);
371
        struct jfs_sb_info *sbi = JFS_SBI(sb);
372
        int nblocks;
373
        s64 blkno;
374
        char *cp = (char *) ealist;
375
        int i;
376
        int nbytes, nb;
377
        s32 bytes_to_read;
378
        struct metapage *mp;
379
 
380
        /* quick check for in-line EA */
381
        if (ji->ea.flag & DXD_INLINE)
382
                return ea_read_inline(ip, ealist);
383
 
384
        nbytes = sizeDXD(&ji->ea);
385
        if (!nbytes) {
386
                jfs_error(sb, "ea_read: nbytes is 0");
387
                return -EIO;
388
        }
389
 
390
        /*
391
         * Figure out how many blocks were allocated when this EA list was
392
         * originally written to disk.
393
         */
394
        nblocks = lengthDXD(&ji->ea) << sbi->l2nbperpage;
395
        blkno = addressDXD(&ji->ea) << sbi->l2nbperpage;
396
 
397
        /*
398
         * I have found the disk blocks which were originally used to store
399
         * the FEALIST.  now i loop over each contiguous block copying the
400
         * data into the buffer.
401
         */
402
        for (i = 0; i < nblocks; i += sbi->nbperpage) {
403
                /*
404
                 * Determine how many bytes for this request, and round up to
405
                 * the nearest aggregate block size
406
                 */
407
                nb = min(PSIZE, nbytes);
408
                bytes_to_read =
409
                    ((((nb + sb->s_blocksize - 1)) >> sb->s_blocksize_bits))
410
                    << sb->s_blocksize_bits;
411
 
412
                if (!(mp = read_metapage(ip, blkno + i, bytes_to_read, 1)))
413
                        return -EIO;
414
 
415
                memcpy(cp, mp->data, nb);
416
                release_metapage(mp);
417
 
418
                cp += PSIZE;
419
                nbytes -= nb;
420
        }
421
 
422
        return 0;
423
}
424
 
425
/*
426
 * NAME: ea_get
427
 *
428
 * FUNCTION: Returns buffer containing existing extended attributes.
429
 *           The size of the buffer will be the larger of the existing
430
 *           attributes size, or min_size.
431
 *
432
 *           The buffer, which may be inlined in the inode or in the
433
 *           page cache must be release by calling ea_release or ea_put
434
 *
435
 * PARAMETERS:
436
 *      inode   - Inode pointer
437
 *      ea_buf  - Structure to be populated with ealist and its metadata
438
 *      min_size- minimum size of buffer to be returned
439
 *
440
 * RETURNS: 0 for success; Other indicates failure
441
 */
442
static int ea_get(struct inode *inode, struct ea_buffer *ea_buf, int min_size)
443
{
444
        struct jfs_inode_info *ji = JFS_IP(inode);
445
        struct super_block *sb = inode->i_sb;
446
        int size;
447
        int ea_size = sizeDXD(&ji->ea);
448
        int blocks_needed, current_blocks;
449
        s64 blkno;
450
        int rc;
451
 
452
        /* When fsck.jfs clears a bad ea, it doesn't clear the size */
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
                 * To keep the rest of the code simple.  Allocate a
496
                 * contiguous buffer to work with
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
                rc = dbAlloc(inode, INOHINT(inode), (s64) blocks_needed,
521
                             &blkno);
522
                if (rc)
523
                        return rc;
524
 
525
                DXDlength(&ea_buf->new_ea, blocks_needed);
526
                DXDaddress(&ea_buf->new_ea, blkno);
527
                ea_buf->new_ea.flag = DXD_EXTENT;
528
                DXDsize(&ea_buf->new_ea, min_size);
529
 
530
                ea_buf->flag = EA_EXTENT | EA_NEW;
531
 
532
                ea_buf->mp = get_metapage(inode, blkno,
533
                                          blocks_needed << sb->s_blocksize_bits,
534
                                          1);
535
                if (ea_buf->mp == NULL) {
536
                        dbFree(inode, blkno, (s64) blocks_needed);
537
                        return -EIO;
538
                }
539
                ea_buf->xattr = ea_buf->mp->data;
540
                ea_buf->max_size = (min_size + sb->s_blocksize - 1) &
541
                    ~(sb->s_blocksize - 1);
542
                if (ea_size == 0)
543
                        return 0;
544
                if ((rc = ea_read(inode, ea_buf->xattr))) {
545
                        discard_metapage(ea_buf->mp);
546
                        dbFree(inode, blkno, (s64) blocks_needed);
547
                        return rc;
548
                }
549
                goto size_check;
550
        }
551
        ea_buf->flag = EA_EXTENT;
552
        ea_buf->mp = read_metapage(inode, addressDXD(&ji->ea),
553
                                   lengthDXD(&ji->ea), 1);
554
        if (ea_buf->mp == NULL)
555
                return -EIO;
556
        ea_buf->xattr = ea_buf->mp->data;
557
        ea_buf->max_size = (ea_size + sb->s_blocksize - 1) &
558
            ~(sb->s_blocksize - 1);
559
 
560
      size_check:
561
        if (EALIST_SIZE(ea_buf->xattr) != ea_size) {
562
                printk(KERN_ERR "ea_get: invalid extended attribute\n");
563
                dump_mem("xattr", ea_buf->xattr, ea_size);
564
                ea_release(inode, ea_buf);
565
                return -EIO;
566
        }
567
 
568
        return ea_size;
569
}
570
 
571
static void ea_release(struct inode *inode, struct ea_buffer *ea_buf)
572
{
573
        if (ea_buf->flag & EA_MALLOC)
574
                kfree(ea_buf->xattr);
575
        else if (ea_buf->flag & EA_EXTENT) {
576
                assert(ea_buf->mp);
577
                release_metapage(ea_buf->mp);
578
 
579
                if (ea_buf->flag & EA_NEW)
580
                        dbFree(inode, addressDXD(&ea_buf->new_ea),
581
                               lengthDXD(&ea_buf->new_ea));
582
        }
583
}
584
 
585
static int ea_put(struct inode *inode, struct ea_buffer *ea_buf, int new_size)
586
{
587
        struct jfs_inode_info *ji = JFS_IP(inode);
588
        unsigned long old_blocks, new_blocks;
589
        int rc = 0;
590
        tid_t tid;
591
 
592
        if (new_size == 0) {
593
                ea_release(inode, ea_buf);
594
                ea_buf = 0;
595
        } else if (ea_buf->flag & EA_INLINE) {
596
                assert(new_size <= sizeof (ji->i_inline_ea));
597
                ji->mode2 &= ~INLINEEA;
598
                ea_buf->new_ea.flag = DXD_INLINE;
599
                DXDsize(&ea_buf->new_ea, new_size);
600
                DXDaddress(&ea_buf->new_ea, 0);
601
                DXDlength(&ea_buf->new_ea, 0);
602
        } else if (ea_buf->flag & EA_MALLOC) {
603
                rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
604
                kfree(ea_buf->xattr);
605
        } else if (ea_buf->flag & EA_NEW) {
606
                /* We have already allocated a new dxd */
607
                flush_metapage(ea_buf->mp);
608
        } else {
609
                /* ->xattr must point to original ea's metapage */
610
                rc = ea_write(inode, ea_buf->xattr, new_size, &ea_buf->new_ea);
611
                discard_metapage(ea_buf->mp);
612
        }
613
        if (rc)
614
                return rc;
615
 
616
        tid = txBegin(inode->i_sb, 0);
617
        down(&ji->commit_sem);
618
 
619
        old_blocks = new_blocks = 0;
620
 
621
        if (ji->ea.flag & DXD_EXTENT) {
622
                invalidate_dxd_metapages(inode, ji->ea);
623
                old_blocks = lengthDXD(&ji->ea);
624
        }
625
 
626
        if (ea_buf) {
627
                txEA(tid, inode, &ji->ea, &ea_buf->new_ea);
628
                if (ea_buf->new_ea.flag & DXD_EXTENT) {
629
                        new_blocks = lengthDXD(&ea_buf->new_ea);
630
                        if (ji->ea.flag & DXD_INLINE)
631
                                ji->mode2 |= INLINEEA;
632
                }
633
                ji->ea = ea_buf->new_ea;
634
        } else {
635
                txEA(tid, inode, &ji->ea, 0);
636
                if (ji->ea.flag & DXD_INLINE)
637
                        ji->mode2 |= INLINEEA;
638
                ji->ea.flag = 0;
639
                ji->ea.size = 0;
640
        }
641
 
642
        inode->i_blocks += LBLK2PBLK(inode->i_sb, new_blocks - old_blocks);
643
        rc = txCommit(tid, 1, &inode, 0);
644
        txEnd(tid);
645
        up(&ji->commit_sem);
646
 
647
        return rc;
648
}
649
 
650
static int can_set_xattr(struct inode *inode, const char *name,
651
                         void *value, size_t value_len)
652
{
653
        if (IS_RDONLY(inode))
654
                return -EROFS;
655
 
656
        if (IS_IMMUTABLE(inode) || IS_APPEND(inode) || S_ISLNK(inode->i_mode))
657
                return -EPERM;
658
 
659
        if((strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN) != 0) &&
660
           (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) != 0))
661
                return -EOPNOTSUPP;
662
 
663
        if (!S_ISREG(inode->i_mode) &&
664
            (!S_ISDIR(inode->i_mode) || inode->i_mode &S_ISVTX))
665
                return -EPERM;
666
 
667
        return permission(inode, MAY_WRITE);
668
}
669
 
670
int __jfs_setxattr(struct inode *inode, const char *name, void *value,
671
                   size_t value_len, int flags)
672
{
673
        struct jfs_ea_list *ealist;
674
        struct jfs_ea *ea, *old_ea = NULL, *next_ea = NULL;
675
        struct ea_buffer ea_buf;
676
        int old_ea_size = 0;
677
        int xattr_size;
678
        int new_size;
679
        int namelen = strlen(name);
680
        char *os2name = NULL;
681
        int found = 0;
682
        int rc;
683
        int length;
684
 
685
        if ((rc = can_set_xattr(inode, name, value, value_len)))
686
                return rc;
687
 
688
        if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
689
                os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
690
                                  GFP_KERNEL);
691
                if (!os2name)
692
                        return -ENOMEM;
693
                strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
694
                name = os2name;
695
                namelen -= XATTR_OS2_PREFIX_LEN;
696
        }
697
 
698
        xattr_size = ea_get(inode, &ea_buf, 0);
699
        if (xattr_size < 0) {
700
                rc = xattr_size;
701
                goto out;
702
        }
703
 
704
      again:
705
        ealist = (struct jfs_ea_list *) ea_buf.xattr;
706
        new_size = sizeof (struct jfs_ea_list);
707
 
708
        if (xattr_size) {
709
                for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist);
710
                     ea = NEXT_EA(ea)) {
711
                        if ((namelen == ea->namelen) &&
712
                            (memcmp(name, ea->name, namelen) == 0)) {
713
                                found = 1;
714
                                if (flags & XATTR_CREATE) {
715
                                        rc = -EEXIST;
716
                                        goto release;
717
                                }
718
                                old_ea = ea;
719
                                old_ea_size = EA_SIZE(ea);
720
                                next_ea = NEXT_EA(ea);
721
                        } else
722
                                new_size += EA_SIZE(ea);
723
                }
724
        }
725
 
726
        if (!found) {
727
                if (flags & XATTR_REPLACE) {
728
                        rc = -ENODATA;
729
                        goto release;
730
                }
731
                if (value == NULL) {
732
                        rc = 0;
733
                        goto release;
734
                }
735
        }
736
        if (value)
737
                new_size += sizeof (struct jfs_ea) + namelen + 1 + value_len;
738
 
739
        if (new_size > ea_buf.max_size) {
740
                /*
741
                 * We need to allocate more space for merged ea list.
742
                 * We should only have loop to again: once.
743
                 */
744
                ea_release(inode, &ea_buf);
745
                xattr_size = ea_get(inode, &ea_buf, new_size);
746
                if (xattr_size < 0) {
747
                        rc = xattr_size;
748
                        goto out;
749
                }
750
                goto again;
751
        }
752
 
753
        /* Remove old ea of the same name */
754
        if (found) {
755
                /* number of bytes following target EA */
756
                length = (char *) END_EALIST(ealist) - (char *) next_ea;
757
                if (length > 0)
758
                        memmove(old_ea, next_ea, length);
759
                xattr_size -= old_ea_size;
760
        }
761
 
762
        /* Add new entry to the end */
763
        if (value) {
764
                if (xattr_size == 0)
765
                        /* Completely new ea list */
766
                        xattr_size = sizeof (struct jfs_ea_list);
767
 
768
                ea = (struct jfs_ea *) ((char *) ealist + xattr_size);
769
                ea->flag = 0;
770
                ea->namelen = namelen;
771
                ea->valuelen = (cpu_to_le16(value_len));
772
                memcpy(ea->name, name, namelen);
773
                ea->name[namelen] = 0;
774
                if (value_len)
775
                        memcpy(&ea->name[namelen + 1], value, value_len);
776
                xattr_size += EA_SIZE(ea);
777
        }
778
 
779
        /* DEBUG - If we did this right, these number match */
780
        if (xattr_size != new_size) {
781
                printk(KERN_ERR
782
                       "jfs_xsetattr: xattr_size = %d, new_size = %d\n",
783
                       xattr_size, new_size);
784
 
785
                rc = -EINVAL;
786
                goto release;
787
        }
788
 
789
        /*
790
         * If we're left with an empty list, there's no ea
791
         */
792
        if (new_size == sizeof (struct jfs_ea_list))
793
                new_size = 0;
794
 
795
        ealist->size = cpu_to_le32(new_size);
796
 
797
        rc = ea_put(inode, &ea_buf, new_size);
798
 
799
        goto out;
800
      release:
801
        ea_release(inode, &ea_buf);
802
      out:
803
        if (os2name)
804
                kfree(os2name);
805
 
806
        return rc;
807
}
808
 
809
int jfs_setxattr(struct dentry *dentry, const char *name, void *value,
810
                 size_t value_len, int flags)
811
{
812
        if (value == NULL) {    /* empty EA, do not remove */
813
                value = "";
814
                value_len = 0;
815
        }
816
 
817
        return __jfs_setxattr(dentry->d_inode, name, value, value_len, flags);
818
}
819
 
820
static inline int can_get_xattr(struct inode *inode, const char *name)
821
{
822
        return permission(inode, MAY_READ);
823
}
824
 
825
ssize_t __jfs_getxattr(struct inode *inode, const char *name, void *data,
826
                       size_t buf_size)
827
{
828
        struct jfs_ea_list *ealist;
829
        struct jfs_ea *ea;
830
        struct ea_buffer ea_buf;
831
        int xattr_size;
832
        ssize_t size;
833
        int namelen = strlen(name);
834
        char *os2name = NULL;
835
        int rc;
836
        char *value;
837
 
838
        if ((rc = can_get_xattr(inode, name)))
839
                return rc;
840
 
841
        if (strncmp(name, XATTR_OS2_PREFIX, XATTR_OS2_PREFIX_LEN) == 0) {
842
                os2name = kmalloc(namelen - XATTR_OS2_PREFIX_LEN + 1,
843
                                  GFP_KERNEL);
844
                if (!os2name)
845
                        return -ENOMEM;
846
                strcpy(os2name, name + XATTR_OS2_PREFIX_LEN);
847
                name = os2name;
848
                namelen -= XATTR_OS2_PREFIX_LEN;
849
        }
850
 
851
        xattr_size = ea_get(inode, &ea_buf, 0);
852
        if (xattr_size < 0) {
853
                size = xattr_size;
854
                goto out;
855
        }
856
 
857
        if (xattr_size == 0)
858
                goto not_found;
859
 
860
        ealist = (struct jfs_ea_list *) ea_buf.xattr;
861
 
862
        /* Find the named attribute */
863
        for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
864
                if ((namelen == ea->namelen) &&
865
                    memcmp(name, ea->name, namelen) == 0) {
866
                        /* Found it */
867
                        size = le16_to_cpu(ea->valuelen);
868
                        if (!data)
869
                                goto release;
870
                        else if (size > buf_size) {
871
                                size = -ERANGE;
872
                                goto release;
873
                        }
874
                        value = ((char *) &ea->name) + ea->namelen + 1;
875
                        memcpy(data, value, size);
876
                        goto release;
877
                }
878
      not_found:
879
        size = -ENODATA;
880
      release:
881
        ea_release(inode, &ea_buf);
882
      out:
883
        if (os2name)
884
                kfree(os2name);
885
 
886
        return size;
887
}
888
 
889
ssize_t jfs_getxattr(struct dentry *dentry, const char *name, void *data,
890
                     size_t buf_size)
891
{
892
        return __jfs_getxattr(dentry->d_inode, name, data, buf_size);
893
}
894
 
895
ssize_t jfs_listxattr(struct dentry * dentry, char *data, size_t buf_size)
896
{
897
        struct inode *inode = dentry->d_inode;
898
        char *buffer;
899
        ssize_t size = 0;
900
        int xattr_size;
901
        struct jfs_ea_list *ealist;
902
        struct jfs_ea *ea;
903
        struct ea_buffer ea_buf;
904
 
905
        xattr_size = ea_get(inode, &ea_buf, 0);
906
        if (xattr_size < 0) {
907
                size = xattr_size;
908
                goto out;
909
        }
910
 
911
        if (xattr_size == 0)
912
                goto release;
913
 
914
        ealist = (struct jfs_ea_list *) ea_buf.xattr;
915
 
916
        /* compute required size of list */
917
        for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea))
918
                size += name_size(ea) + 1;
919
 
920
        if (!data)
921
                goto release;
922
 
923
        if (size > buf_size) {
924
                size = -ERANGE;
925
                goto release;
926
        }
927
 
928
        /* Copy attribute names to buffer */
929
        buffer = data;
930
        for (ea = FIRST_EA(ealist); ea < END_EALIST(ealist); ea = NEXT_EA(ea)) {
931
                int namelen = copy_name(buffer, ea);
932
                buffer += namelen + 1;
933
        }
934
 
935
      release:
936
        ea_release(inode, &ea_buf);
937
      out:
938
        return size;
939
}
940
 
941
int jfs_removexattr(struct dentry *dentry, const char *name)
942
{
943
        return __jfs_setxattr(dentry->d_inode, name, 0, 0, XATTR_REPLACE);
944
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.