4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA
24 * Copyright (c) 2013, Intel Corporation.
25 * Use is subject to license terms.
27 * Author: Di Wang <di.wang@intel.com>
30 #include <lustre/lustre_idl.h>
32 #include <lustre_linkea.h>
34 int linkea_data_new(struct linkea_data *ldata, struct lu_buf *buf)
36 ldata->ld_buf = lu_buf_check_and_alloc(buf, PAGE_CACHE_SIZE);
37 if (ldata->ld_buf->lb_buf == NULL)
39 ldata->ld_leh = ldata->ld_buf->lb_buf;
40 ldata->ld_leh->leh_magic = LINK_EA_MAGIC;
41 ldata->ld_leh->leh_len = sizeof(struct link_ea_header);
42 ldata->ld_leh->leh_reccount = 0;
45 EXPORT_SYMBOL(linkea_data_new);
47 int linkea_init(struct linkea_data *ldata)
49 struct link_ea_header *leh;
51 LASSERT(ldata->ld_buf != NULL);
52 leh = ldata->ld_buf->lb_buf;
53 if (leh->leh_magic == __swab32(LINK_EA_MAGIC)) {
54 leh->leh_magic = LINK_EA_MAGIC;
55 leh->leh_reccount = __swab32(leh->leh_reccount);
56 leh->leh_len = __swab64(leh->leh_len);
57 /* entries are swabbed by linkea_entry_unpack */
59 if (leh->leh_magic != LINK_EA_MAGIC)
61 if (leh->leh_reccount == 0)
67 EXPORT_SYMBOL(linkea_init);
70 * Pack a link_ea_entry.
71 * All elements are stored as chars to avoid alignment issues.
72 * Numbers are always big-endian
73 * \retval record length
75 static int linkea_entry_pack(struct link_ea_entry *lee,
76 const struct lu_name *lname,
77 const struct lu_fid *pfid)
82 fid_cpu_to_be(&tmpfid, pfid);
83 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LINKEA_CRASH))
85 memcpy(&lee->lee_parent_fid, &tmpfid, sizeof(tmpfid));
86 memcpy(lee->lee_name, lname->ln_name, lname->ln_namelen);
87 reclen = sizeof(struct link_ea_entry) + lname->ln_namelen;
89 lee->lee_reclen[0] = (reclen >> 8) & 0xff;
90 lee->lee_reclen[1] = reclen & 0xff;
94 void linkea_entry_unpack(const struct link_ea_entry *lee, int *reclen,
95 struct lu_name *lname, struct lu_fid *pfid)
97 *reclen = (lee->lee_reclen[0] << 8) | lee->lee_reclen[1];
98 memcpy(pfid, &lee->lee_parent_fid, sizeof(*pfid));
99 fid_be_to_cpu(pfid, pfid);
100 lname->ln_name = lee->lee_name;
101 lname->ln_namelen = *reclen - sizeof(struct link_ea_entry);
103 EXPORT_SYMBOL(linkea_entry_unpack);
106 * Add a record to the end of link ea buf
108 int linkea_add_buf(struct linkea_data *ldata, const struct lu_name *lname,
109 const struct lu_fid *pfid)
111 LASSERT(ldata->ld_leh != NULL);
113 if (lname == NULL || pfid == NULL)
116 ldata->ld_reclen = lname->ln_namelen + sizeof(struct link_ea_entry);
117 if (ldata->ld_leh->leh_len + ldata->ld_reclen >
118 ldata->ld_buf->lb_len) {
119 if (lu_buf_check_and_grow(ldata->ld_buf,
120 ldata->ld_leh->leh_len +
121 ldata->ld_reclen) < 0)
125 ldata->ld_leh = ldata->ld_buf->lb_buf;
126 ldata->ld_lee = ldata->ld_buf->lb_buf + ldata->ld_leh->leh_len;
127 ldata->ld_reclen = linkea_entry_pack(ldata->ld_lee, lname, pfid);
128 ldata->ld_leh->leh_len += ldata->ld_reclen;
129 ldata->ld_leh->leh_reccount++;
130 CDEBUG(D_INODE, "New link_ea name '%.*s' is added\n",
131 lname->ln_namelen, lname->ln_name);
134 EXPORT_SYMBOL(linkea_add_buf);
136 /** Del the current record from the link ea buf */
137 void linkea_del_buf(struct linkea_data *ldata, const struct lu_name *lname)
139 LASSERT(ldata->ld_leh != NULL && ldata->ld_lee != NULL);
141 ldata->ld_leh->leh_reccount--;
142 ldata->ld_leh->leh_len -= ldata->ld_reclen;
143 memmove(ldata->ld_lee, (char *)ldata->ld_lee + ldata->ld_reclen,
144 (char *)ldata->ld_leh + ldata->ld_leh->leh_len -
145 (char *)ldata->ld_lee);
146 CDEBUG(D_INODE, "Old link_ea name '%.*s' is removed\n",
147 lname->ln_namelen, lname->ln_name);
149 if ((char *)ldata->ld_lee >= ((char *)ldata->ld_leh +
150 ldata->ld_leh->leh_len))
151 ldata->ld_lee = NULL;
153 EXPORT_SYMBOL(linkea_del_buf);
156 * Check if such a link exists in linkEA.
158 * \param ldata link data the search to be done on
159 * \param lname name in the parent's directory entry pointing to this object
160 * \param pfid parent fid the link to be found for
163 * \retval -ENOENT link does not exist
164 * \retval -ve on error
166 int linkea_links_find(struct linkea_data *ldata, const struct lu_name *lname,
167 const struct lu_fid *pfid)
169 struct lu_name tmpname;
170 struct lu_fid tmpfid;
173 LASSERT(ldata->ld_leh != NULL);
176 ldata->ld_lee = (struct link_ea_entry *)(ldata->ld_leh + 1);
178 for (count = 0; count < ldata->ld_leh->leh_reccount; count++) {
179 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen,
181 if (tmpname.ln_namelen == lname->ln_namelen &&
182 lu_fid_eq(&tmpfid, pfid) &&
183 (strncmp(tmpname.ln_name, lname->ln_name,
184 tmpname.ln_namelen) == 0))
186 ldata->ld_lee = (struct link_ea_entry *)((char *)ldata->ld_lee +
190 if (count == ldata->ld_leh->leh_reccount) {
191 CDEBUG(D_INODE, "Old link_ea name '%.*s' not found\n",
192 lname->ln_namelen, lname->ln_name);
193 ldata->ld_lee = NULL;
194 ldata->ld_reclen = 0;
199 EXPORT_SYMBOL(linkea_links_find);