cregit-Linux how code gets into the kernel

Release 4.10 fs/gfs2/xattr.c

Directory: fs/gfs2
/*
 * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
 * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU General Public License version 2.
 */

#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/completion.h>
#include <linux/buffer_head.h>
#include <linux/xattr.h>
#include <linux/gfs2_ondisk.h>
#include <linux/posix_acl_xattr.h>
#include <linux/uaccess.h>

#include "gfs2.h"
#include "incore.h"
#include "acl.h"
#include "xattr.h"
#include "glock.h"
#include "inode.h"
#include "meta_io.h"
#include "quota.h"
#include "rgrp.h"
#include "trans.h"
#include "util.h"

/**
 * ea_calc_size - returns the acutal number of bytes the request will take up
 *                (not counting any unstuffed data blocks)
 * @sdp:
 * @er:
 * @size:
 *
 * Returns: 1 if the EA should be stuffed
 */


static int ea_calc_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize, unsigned int *size) { unsigned int jbsize = sdp->sd_jbsize; /* Stuffed */ *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + dsize, 8); if (*size <= jbsize) return 1; /* Unstuffed */ *size = ALIGN(sizeof(struct gfs2_ea_header) + nsize + (sizeof(__be64) * DIV_ROUND_UP(dsize, jbsize)), 8); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
steven whitehousesteven whitehouse5255.32%150.00%
david teiglanddavid teigland4244.68%150.00%
Total94100.00%2100.00%


static int ea_check_size(struct gfs2_sbd *sdp, unsigned int nsize, size_t dsize) { unsigned int size; if (dsize > GFS2_EA_MAX_DATA_LEN) return -ERANGE; ea_calc_size(sdp, nsize, dsize, &size); /* This can only happen with 512 byte blocks */ if (size > sdp->sd_jbsize) return -ERANGE; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland5083.33%150.00%
steven whitehousesteven whitehouse1016.67%150.00%
Total60100.00%2100.00%

typedef int (*ea_call_t) (struct gfs2_inode *ip, struct buffer_head *bh, struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, void *private);
static int ea_foreach_i(struct gfs2_inode *ip, struct buffer_head *bh, ea_call_t ea_call, void *data) { struct gfs2_ea_header *ea, *prev = NULL; int error = 0; if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_EA)) return -EIO; for (ea = GFS2_EA_BH2FIRST(bh);; prev = ea, ea = GFS2_EA2NEXT(ea)) { if (!GFS2_EA_REC_LEN(ea)) goto fail; if (!(bh->b_data <= (char *)ea && (char *)GFS2_EA2NEXT(ea) <= bh->b_data + bh->b_size)) goto fail; if (!GFS2_EATYPE_VALID(ea->ea_type)) goto fail; error = ea_call(ip, bh, ea, prev, data); if (error) return error; if (GFS2_EA_IS_LAST(ea)) { if ((char *)GFS2_EA2NEXT(ea) != bh->b_data + bh->b_size) goto fail; break; } } return error; fail: gfs2_consist_inode(ip); return -EIO; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland20497.61%150.00%
steven whitehousesteven whitehouse52.39%150.00%
Total209100.00%2100.00%


static int ea_foreach(struct gfs2_inode *ip, ea_call_t ea_call, void *data) { struct buffer_head *bh, *eabh; __be64 *eablk, *end; int error; error = gfs2_meta_read(ip->i_gl, ip->i_eattr, DIO_WAIT, 0, &bh); if (error) return error; if (!(ip->i_diskflags & GFS2_DIF_EA_INDIRECT)) { error = ea_foreach_i(ip, bh, ea_call, data); goto out; } if (gfs2_metatype_check(GFS2_SB(&ip->i_inode), bh, GFS2_METATYPE_IN)) { error = -EIO; goto out; } eablk = (__be64 *)(bh->b_data + sizeof(struct gfs2_meta_header)); end = eablk + GFS2_SB(&ip->i_inode)->sd_inptrs; for (; eablk < end; eablk++) { u64 bn; if (!*eablk) break; bn = be64_to_cpu(*eablk); error = gfs2_meta_read(ip->i_gl, bn, DIO_WAIT, 0, &eabh); if (error) break; error = ea_foreach_i(ip, eabh, ea_call, data); brelse(eabh); if (error) break; } out: brelse(bh); return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland21891.98%114.29%
steven whitehousesteven whitehouse135.49%457.14%
andreas gruenbacherandreas gruenbacher41.69%114.29%
al viroal viro20.84%114.29%
Total237100.00%7100.00%

struct ea_find { int type; const char *name; size_t namel; struct gfs2_ea_location *ef_el; };
static int ea_find_i(struct gfs2_inode *ip, struct buffer_head *bh, struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, void *private) { struct ea_find *ef = private; if (ea->ea_type == GFS2_EATYPE_UNUSED) return 0; if (ea->ea_type == ef->type) { if (ea->ea_name_len == ef->namel && !memcmp(GFS2_EA2NAME(ea), ef->name, ea->ea_name_len)) { struct gfs2_ea_location *el = ef->ef_el; get_bh(bh); el->el_bh = bh; el->el_ea = ea; el->el_prev = prev; return 1; } } return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland12195.28%150.00%
steven whitehousesteven whitehouse64.72%150.00%
Total127100.00%2100.00%


static int gfs2_ea_find(struct gfs2_inode *ip, int type, const char *name, struct gfs2_ea_location *el) { struct ea_find ef; int error; ef.type = type; ef.name = name; ef.namel = strlen(name); ef.ef_el = el; memset(el, 0, sizeof(struct gfs2_ea_location)); error = ea_foreach(ip, ea_find_i, &ef); if (error > 0) return 0; return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland7174.74%133.33%
steven whitehousesteven whitehouse2425.26%266.67%
Total95100.00%3100.00%

/** * ea_dealloc_unstuffed - * @ip: * @bh: * @ea: * @prev: * @private: * * Take advantage of the fact that all unstuffed blocks are * allocated from the same RG. But watch, this may not always * be true. * * Returns: errno */
static int ea_dealloc_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh, struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, void *private) { int *leave = private; struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); struct gfs2_rgrpd *rgd; struct gfs2_holder rg_gh; struct buffer_head *dibh; __be64 *dataptrs; u64 bn = 0; u64 bstart = 0; unsigned int blen = 0; unsigned int blks = 0; unsigned int x; int error; error = gfs2_rindex_update(sdp); if (error) return error; if (GFS2_EA_IS_STUFFED(ea)) return 0; dataptrs = GFS2_EA2DATAPTRS(ea); for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { if (*dataptrs) { blks++; bn = be64_to_cpu(*dataptrs); } } if (!blks) return 0; rgd = gfs2_blk2rgrpd(sdp, bn, 1); if (!rgd) { gfs2_consist_inode(ip); return -EIO; } error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &rg_gh); if (error) return error; error = gfs2_trans_begin(sdp, rgd->rd_length + RES_DINODE + RES_EATTR + RES_STATFS + RES_QUOTA, blks); if (error) goto out_gunlock; gfs2_trans_add_meta(ip->i_gl, bh); dataptrs = GFS2_EA2DATAPTRS(ea); for (x = 0; x < ea->ea_num_ptrs; x++, dataptrs++) { if (!*dataptrs) break; bn = be64_to_cpu(*dataptrs); if (bstart + blen == bn) blen++; else { if (bstart) gfs2_free_meta(ip, bstart, blen); bstart = bn; blen = 1; } *dataptrs = 0; gfs2_add_inode_blocks(&ip->i_inode, -1); } if (bstart) gfs2_free_meta(ip, bstart, blen); if (prev && !leave) { u32 len; len = GFS2_EA_REC_LEN(prev) + GFS2_EA_REC_LEN(ea); prev->ea_rec_len = cpu_to_be32(len); if (GFS2_EA_IS_LAST(ea)) prev->ea_flags |= GFS2_EAFLAG_LAST; } else { ea->ea_type = GFS2_EATYPE_UNUSED; ea->ea_num_ptrs = 0; } error = gfs2_meta_inode_buffer(ip, &dibh); if (!error) { ip->i_inode.i_ctime = current_time(&ip->i_inode); gfs2_trans_add_meta(ip->i_gl, dibh); gfs2_dinode_out(ip, dibh->b_data); brelse(dibh); } gfs2_trans_end(sdp); out_gunlock: gfs2_glock_dq_uninit(&rg_gh); return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland44689.74%17.69%
steven whitehousesteven whitehouse275.43%969.23%
robert s. petersonrobert s. peterson142.82%17.69%
deepa dinamanideepa dinamani71.41%17.69%
al viroal viro30.60%17.69%
Total497100.00%13100.00%


static int ea_remove_unstuffed(struct gfs2_inode *ip, struct buffer_head *bh, struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, int leave) { int error; error = gfs2_rindex_update(GFS2_SB(&ip->i_inode)); if (error) return error; error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); if (error) goto out_alloc; error = ea_dealloc_unstuffed(ip, bh, ea, prev, (leave) ? &error : NULL); gfs2_quota_unhold(ip); out_alloc: return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland8078.43%133.33%
robert s. petersonrobert s. peterson2019.61%133.33%
eric w. biedermaneric w. biederman21.96%133.33%
Total102100.00%3100.00%

struct ea_list { struct gfs2_ea_request *ei_er; unsigned int ei_size; };
static inline unsigned int gfs2_ea_strlen(struct gfs2_ea_header *ea) { switch (ea->ea_type) { case GFS2_EATYPE_USR: return 5 + ea->ea_name_len + 1; case GFS2_EATYPE_SYS: return 7 + ea->ea_name_len + 1; case GFS2_EATYPE_SECURITY: return 9 + ea->ea_name_len + 1; default: return 0; } }

Contributors

PersonTokensPropCommitsCommitProp
steven whitehousesteven whitehouse61100.00%1100.00%
Total61100.00%1100.00%


static int ea_list_i(struct gfs2_inode *ip, struct buffer_head *bh, struct gfs2_ea_header *ea, struct gfs2_ea_header *prev, void *private) { struct ea_list *ei = private; struct gfs2_ea_request *er = ei->ei_er; unsigned int ea_size = gfs2_ea_strlen(ea); if (ea->ea_type == GFS2_EATYPE_UNUSED) return 0; if (er->er_data_len) { char *prefix = NULL; unsigned int l = 0; char c = 0; if (ei->ei_size + ea_size > er->er_data_len) return -ERANGE; switch (ea->ea_type) { case GFS2_EATYPE_USR: prefix = "user."; l = 5; break; case GFS2_EATYPE_SYS: prefix = "system."; l = 7; break; case GFS2_EATYPE_SECURITY: prefix = "security."; l = 9; break; } BUG_ON(l == 0); memcpy(er->er_data + ei->ei_size, prefix, l); memcpy(er->er_data + ei->ei_size + l, GFS2_EA2NAME(ea), ea->ea_name_len); memcpy(er->er_data + ei->ei_size + ea_size - 1, &c, 1); } ei->ei_size += ea_size; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland19185.27%133.33%
ryan o'hararyan o'hara229.82%133.33%
steven whitehousesteven whitehouse114.91%133.33%
Total224100.00%3100.00%

/** * gfs2_listxattr - List gfs2 extended attributes * @dentry: The dentry whose inode we are interested in * @buffer: The buffer to write the results * @size: The size of the buffer * * Returns: actual size of data on success, -errno on error */
ssize_t gfs2_listxattr(struct dentry *dentry, char *buffer, size_t size) { struct gfs2_inode *ip = GFS2_I(d_inode(dentry)); struct gfs2_ea_request er; struct gfs2_holder i_gh; int error; memset(&er, 0, sizeof(struct gfs2_ea_request)); if (size) { er.er_data = buffer; er.er_data_len = size; } error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh); if (error) return error; if (ip->i_eattr) { struct ea_list ei = { .ei_er = &er, .ei_size = 0 }; error = ea_foreach(ip, ea_list_i, &ei); if (!error) error = ei.ei_size; } gfs2_glock_dq_uninit(&i_gh); return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland10769.93%125.00%
steven whitehousesteven whitehouse4328.10%250.00%
david howellsdavid howells31.96%125.00%
Total153100.00%4100.00%

/** * ea_iter_unstuffed - copies the unstuffed xattr data to/from the * request buffer * @ip: The GFS2 inode * @ea: The extended attribute header structure * @din: The data to be copied in * @dout: The data to be copied out (one of din,dout will be NULL) * * Returns: errno */
static int gfs2_iter_unstuffed(struct gfs2_inode *ip, struct gfs2_ea_header *ea, const char *din, char *dout) { struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); struct buffer_head **bh; unsigned int amount = GFS2_EA_DATA_LEN(ea); unsigned int nptrs = DIV_ROUND_UP(amount, sdp->sd_jbsize); __be64 *dataptrs = GFS2_EA2DATAPTRS(ea); unsigned int x; int error = 0; unsigned char *pos; unsigned cp_size; bh = kcalloc(nptrs, sizeof(struct buffer_head *), GFP_NOFS); if (!bh) return -ENOMEM; for (x = 0; x < nptrs; x++) { error = gfs2_meta_read(ip->i_gl, be64_to_cpu(*dataptrs), 0, 0, bh + x); if (error) { while (x--) brelse(bh[x]); goto out; } dataptrs++; } for (x = 0; x < nptrs; x++) { error = gfs2_meta_wait(sdp, bh[x]); if (error) { for (; x < nptrs; x++) brelse(bh[x]); goto out; } if (gfs2_metatype_check(sdp, bh[x], GFS2_METATYPE_ED)) { for (; x < nptrs; x++) brelse(bh[x]); error = -EIO; goto out; } pos = bh[x]->b_data + sizeof(struct gfs2_meta_header); cp_size = (sdp->sd_jbsize > amount) ? amount : sdp->sd_jbsize; if (dout) { memcpy(dout, pos, cp_size); dout += sdp->sd_jbsize; } if (din) { gfs2_trans_add_meta(ip->i_gl, bh[x]); memcpy(pos, din, cp_size); din += sdp->sd_jbsize; } amount -= sdp->sd_jbsize; brelse(bh[x]); } out: kfree(bh); return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland30378.70%111.11%
steven whitehousesteven whitehouse7820.26%555.56%
andreas gruenbacherandreas gruenbacher20.52%111.11%
josef bacikjosef bacik10.26%111.11%
al viroal viro10.26%111.11%
Total385100.00%9100.00%


static int gfs2_ea_get_copy(struct gfs2_inode *ip, struct gfs2_ea_location *el, char *data, size_t size) { int ret; size_t len = GFS2_EA_DATA_LEN(el->el_ea); if (len > size) return -ERANGE; if (GFS2_EA_IS_STUFFED(el->el_ea)) { memcpy(data, GFS2_EA2DATA(el->el_ea), len); return len; } ret = gfs2_iter_unstuffed(ip, el->el_ea, NULL, data); if (ret < 0) return ret; return len; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland5554.46%125.00%
steven whitehousesteven whitehouse4645.54%375.00%
Total101100.00%4100.00%


int gfs2_xattr_acl_get(struct gfs2_inode *ip, const char *name, char **ppdata) { struct gfs2_ea_location el; int error; int len; char *data; error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, &el); if (error) return error; if (!el.el_ea) goto out; if (!GFS2_EA_DATA_LEN(el.el_ea)) goto out; len = GFS2_EA_DATA_LEN(el.el_ea); data = kmalloc(len, GFP_NOFS); error = -ENOMEM; if (data == NULL) goto out; error = gfs2_ea_get_copy(ip, &el, data, len); if (error < 0) kfree(data); else *ppdata = data; out: brelse(el.el_bh); return error; }

Contributors

PersonTokensPropCommitsCommitProp
steven whitehousesteven whitehouse153100.00%2100.00%
Total153100.00%2100.00%

/** * gfs2_xattr_get - Get a GFS2 extended attribute * @inode: The inode * @name: The name of the extended attribute * @buffer: The buffer to write the result into * @size: The size of the buffer * @type: The type of extended attribute * * Returns: actual size of data on success, -errno on error */
static int __gfs2_xattr_get(struct inode *inode, const char *name, void *buffer, size_t size, int type) { struct gfs2_inode *ip = GFS2_I(inode); struct gfs2_ea_location el; int error; if (!ip->i_eattr) return -ENODATA; if (strlen(name) > GFS2_EA_MAX_NAME_LEN) return -EINVAL; error = gfs2_ea_find(ip, type, name, &el); if (error) return error; if (!el.el_ea) return -ENODATA; if (size) error = gfs2_ea_get_copy(ip, &el, buffer, size); else error = GFS2_EA_DATA_LEN(el.el_ea); brelse(el.el_bh); return error; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland8562.04%116.67%
steven whitehousesteven whitehouse4230.66%233.33%
al viroal viro96.57%233.33%
christoph hellwigchristoph hellwig10.73%116.67%
Total137100.00%6100.00%


static int gfs2_xattr_get(const struct xattr_handler *handler, struct dentry *unused, struct inode *inode, const char *name, void *buffer, size_t size) { struct gfs2_inode *ip = GFS2_I(inode); struct gfs2_holder gh; bool need_unlock = false; int ret; /* During lookup, SELinux calls this function with the glock locked. */ if (!gfs2_glock_is_locked_by_me(ip->i_gl)) { ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &gh); if (ret) return ret; need_unlock = true; } ret = __gfs2_xattr_get(inode, name, buffer, size, handler->flags); if (need_unlock) gfs2_glock_dq_uninit(&gh); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
al viroal viro126100.00%1100.00%
Total126100.00%1100.00%

/** * ea_alloc_blk - allocates a new block for extended attributes. * @ip: A pointer to the inode that's getting extended attributes * @bhp: Pointer to pointer to a struct buffer_head * * Returns: errno */
static int ea_alloc_blk(struct gfs2_inode *ip, struct buffer_head **bhp) { struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); struct gfs2_ea_header *ea; unsigned int n = 1; u64 block; int error; error = gfs2_alloc_blocks(ip, &block, &n, 0, NULL); if (error) return error; gfs2_trans_add_unrevoke(sdp, block, 1); *bhp = gfs2_meta_new(ip->i_gl, block); gfs2_trans_add_meta(ip->i_gl, *bhp); gfs2_metatype_set(*bhp, GFS2_METATYPE_EA, GFS2_FORMAT_EA); gfs2_buffer_clear_tail(*bhp, sizeof(struct gfs2_meta_header)); ea = GFS2_EA_BH2FIRST(*bhp); ea->ea_rec_len = cpu_to_be32(sdp->sd_jbsize); ea->ea_type = GFS2_EATYPE_UNUSED; ea->ea_flags = GFS2_EAFLAG_LAST; ea->ea_num_ptrs = 0; gfs2_add_inode_blocks(&ip->i_inode, 1); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
david teiglanddavid teigland12068.97%19.09%
steven whitehousesteven whitehouse4928.16%872.73%
robert s. petersonrobert s. peterson52.87%218.18%
Total174100.00%11100.00%

/** * ea_write - writes the request info to an ea, creating new blocks if * necessary * @ip: inode that is being modified * @ea: the location of the new ea in a block * @er: the write request * * Note: does not update ea_rec_len or the GFS2_EAFLAG_LAST bin of ea_flags * * returns : errno */
static int ea_write(struct gfs2_inode *ip, struct gfs2_ea_header *ea, struct gfs2_ea_request *er) { struct gfs2_sbd