Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/*
 * GPL HEADER START
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 only,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License version 2 for more details (a copy is included
 * in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this program; If not, see
 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 * GPL HEADER END
 */
/*
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright (c) 2012, Intel Corporation.
 */
/*
 * This file is part of Lustre, http://www.lustre.org/
 * Lustre is a trademark of Sun Microsystems, Inc.
 *
 * lustre/obdclass/acl.c
 *
 * Lustre Access Control List.
 *
 * Author: Fan Yong <fanyong@clusterfs.com>
 */

#define DEBUG_SUBSYSTEM S_SEC
#include <lu_object.h>
#include <lustre_acl.h>
#include <lustre_eacl.h>
#include <obd_support.h>

#ifdef CONFIG_FS_POSIX_ACL

#define CFS_ACL_XATTR_VERSION POSIX_ACL_XATTR_VERSION

enum {
	ES_UNK  = 0,    /* unknown stat */
	ES_UNC  = 1,    /* ACL entry is not changed */
	ES_MOD  = 2,    /* ACL entry is modified */
	ES_ADD  = 3,    /* ACL entry is added */
	ES_DEL  = 4     /* ACL entry is deleted */
};

static inline void lustre_ext_acl_le_to_cpu(ext_acl_xattr_entry *d,
					    ext_acl_xattr_entry *s)
{
	d->e_tag	= le16_to_cpu(s->e_tag);
	d->e_perm       = le16_to_cpu(s->e_perm);
	d->e_id	 = le32_to_cpu(s->e_id);
	d->e_stat       = le32_to_cpu(s->e_stat);
}

static inline void lustre_ext_acl_cpu_to_le(ext_acl_xattr_entry *d,
					    ext_acl_xattr_entry *s)
{
	d->e_tag	= cpu_to_le16(s->e_tag);
	d->e_perm       = cpu_to_le16(s->e_perm);
	d->e_id	 = cpu_to_le32(s->e_id);
	d->e_stat       = cpu_to_le32(s->e_stat);
}

static inline void lustre_posix_acl_le_to_cpu(posix_acl_xattr_entry *d,
					      posix_acl_xattr_entry *s)
{
	d->e_tag	= le16_to_cpu(s->e_tag);
	d->e_perm       = le16_to_cpu(s->e_perm);
	d->e_id	 = le32_to_cpu(s->e_id);
}

static inline void lustre_posix_acl_cpu_to_le(posix_acl_xattr_entry *d,
					      posix_acl_xattr_entry *s)
{
	d->e_tag	= cpu_to_le16(s->e_tag);
	d->e_perm       = cpu_to_le16(s->e_perm);
	d->e_id	 = cpu_to_le32(s->e_id);
}


/* if "new_count == 0", then "new = {a_version, NULL}", NOT NULL. */
static int lustre_posix_acl_xattr_reduce_space(posix_acl_xattr_header **header,
					       int old_count, int new_count)
{
	int old_size = CFS_ACL_XATTR_SIZE(old_count, posix_acl_xattr);
	int new_size = CFS_ACL_XATTR_SIZE(new_count, posix_acl_xattr);
	posix_acl_xattr_header *new;

	if (unlikely(old_count <= new_count))
		return old_size;

	OBD_ALLOC(new, new_size);
	if (unlikely(new == NULL))
		return -ENOMEM;

	memcpy(new, *header, new_size);
	OBD_FREE(*header, old_size);
	*header = new;
	return new_size;
}

/* if "new_count == 0", then "new = {0, NULL}", NOT NULL. */
static int lustre_ext_acl_xattr_reduce_space(ext_acl_xattr_header **header,
					     int old_count)
{
	int ext_count = le32_to_cpu((*header)->a_count);
	int ext_size = CFS_ACL_XATTR_SIZE(ext_count, ext_acl_xattr);
	int old_size = CFS_ACL_XATTR_SIZE(old_count, ext_acl_xattr);
	ext_acl_xattr_header *new;

	if (unlikely(old_count <= ext_count))
		return 0;

	OBD_ALLOC(new, ext_size);
	if (unlikely(new == NULL))
		return -ENOMEM;

	memcpy(new, *header, ext_size);
	OBD_FREE(*header, old_size);
	*header = new;
	return 0;
}

/*
 * Generate new extended ACL based on the posix ACL.
 */
ext_acl_xattr_header *
lustre_posix_acl_xattr_2ext(posix_acl_xattr_header *header, int size)
{
	int count, i, esize;
	ext_acl_xattr_header *new;

	if (unlikely(size < 0))
		return ERR_PTR(-EINVAL);
	else if (!size)
		count = 0;
	else
		count = CFS_ACL_XATTR_COUNT(size, posix_acl_xattr);
	esize = CFS_ACL_XATTR_SIZE(count, ext_acl_xattr);
	OBD_ALLOC(new, esize);
	if (unlikely(new == NULL))
		return ERR_PTR(-ENOMEM);

	new->a_count = cpu_to_le32(count);
	for (i = 0; i < count; i++) {
		new->a_entries[i].e_tag  = header->a_entries[i].e_tag;
		new->a_entries[i].e_perm = header->a_entries[i].e_perm;
		new->a_entries[i].e_id   = header->a_entries[i].e_id;
		new->a_entries[i].e_stat = cpu_to_le32(ES_UNK);
	}

	return new;
}
EXPORT_SYMBOL(lustre_posix_acl_xattr_2ext);

/*
 * Filter out the "nobody" entries in the posix ACL.
 */
int lustre_posix_acl_xattr_filter(posix_acl_xattr_header *header, int size,
				  posix_acl_xattr_header **out)
{
	int count, i, j, rc = 0;
	__u32 id;
	posix_acl_xattr_header *new;

	if (unlikely(size < 0))
		return -EINVAL;
	else if (!size)
		return 0;

	OBD_ALLOC(new, size);
	if (unlikely(new == NULL))
		return -ENOMEM;

	new->a_version = cpu_to_le32(CFS_ACL_XATTR_VERSION);
	count = CFS_ACL_XATTR_COUNT(size, posix_acl_xattr);
	for (i = 0, j = 0; i < count; i++) {
		id = le32_to_cpu(header->a_entries[i].e_id);
		switch (le16_to_cpu(header->a_entries[i].e_tag)) {
		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_MASK:
		case ACL_OTHER:
			if (id != ACL_UNDEFINED_ID)
				GOTO(_out, rc = -EIO);

			memcpy(&new->a_entries[j++], &header->a_entries[i],
			       sizeof(posix_acl_xattr_entry));
			break;
		case ACL_USER:
			if (id != NOBODY_UID)
				memcpy(&new->a_entries[j++],
				       &header->a_entries[i],
				       sizeof(posix_acl_xattr_entry));
			break;
		case ACL_GROUP:
			if (id != NOBODY_GID)
				memcpy(&new->a_entries[j++],
				       &header->a_entries[i],
				       sizeof(posix_acl_xattr_entry));
			break;
		default:
			GOTO(_out, rc = -EIO);
		}
	}

	/* free unused space. */
	rc = lustre_posix_acl_xattr_reduce_space(&new, count, j);
	if (rc >= 0) {
		size = rc;
		*out = new;
		rc = 0;
	}

_out:
	if (rc) {
		OBD_FREE(new, size);
		size = rc;
	}
	return size;
}
EXPORT_SYMBOL(lustre_posix_acl_xattr_filter);

/*
 * Release the posix ACL space.
 */
void lustre_posix_acl_xattr_free(posix_acl_xattr_header *header, int size)
{
	OBD_FREE(header, size);
}
EXPORT_SYMBOL(lustre_posix_acl_xattr_free);

/*
 * Release the extended ACL space.
 */
void lustre_ext_acl_xattr_free(ext_acl_xattr_header *header)
{
	OBD_FREE(header, CFS_ACL_XATTR_SIZE(le32_to_cpu(header->a_count), \
					    ext_acl_xattr));
}
EXPORT_SYMBOL(lustre_ext_acl_xattr_free);

static ext_acl_xattr_entry *
lustre_ext_acl_xattr_search(ext_acl_xattr_header *header,
			    posix_acl_xattr_entry *entry, int *pos)
{
	int once, start, end, i, j, count = le32_to_cpu(header->a_count);

	once = 0;
	start = *pos;
	end = count;

again:
	for (i = start; i < end; i++) {
		if (header->a_entries[i].e_tag == entry->e_tag &&
		    header->a_entries[i].e_id == entry->e_id) {
			j = i;
			if (++i >= count)
				i = 0;
			*pos = i;
			return &header->a_entries[j];
		}
	}

	if (!once) {
		once = 1;
		start = 0;
		end = *pos;
		goto again;
	}

	return NULL;
}

/*
 * Merge the posix ACL and the extended ACL into new posix ACL.
 */
int lustre_acl_xattr_merge2posix(posix_acl_xattr_header *posix_header, int size,
				 ext_acl_xattr_header *ext_header,
				 posix_acl_xattr_header **out)
{
	int posix_count, posix_size, i, j;
	int ext_count = le32_to_cpu(ext_header->a_count), pos = 0, rc = 0;
	posix_acl_xattr_entry pe = {ACL_MASK, 0, ACL_UNDEFINED_ID};
	posix_acl_xattr_header *new;
	ext_acl_xattr_entry *ee, ae;

	lustre_posix_acl_cpu_to_le(&pe, &pe);
	ee = lustre_ext_acl_xattr_search(ext_header, &pe, &pos);
	if (ee == NULL || le32_to_cpu(ee->e_stat) == ES_DEL) {
		/* there are only base ACL entries at most. */
		posix_count = 3;
		posix_size = CFS_ACL_XATTR_SIZE(posix_count, posix_acl_xattr);
		OBD_ALLOC(new, posix_size);
		if (unlikely(new == NULL))
			return -ENOMEM;

		new->a_version = cpu_to_le32(CFS_ACL_XATTR_VERSION);
		for (i = 0, j = 0; i < ext_count; i++) {
			lustre_ext_acl_le_to_cpu(&ae,
						 &ext_header->a_entries[i]);
			switch (ae.e_tag) {
			case ACL_USER_OBJ:
			case ACL_GROUP_OBJ:
			case ACL_OTHER:
				if (ae.e_id != ACL_UNDEFINED_ID)
					GOTO(_out, rc = -EIO);

				if (ae.e_stat != ES_DEL) {
					new->a_entries[j].e_tag =
						ext_header->a_entries[i].e_tag;
					new->a_entries[j].e_perm =
						ext_header->a_entries[i].e_perm;
					new->a_entries[j++].e_id =
						ext_header->a_entries[i].e_id;
				}
				break;
			case ACL_MASK:
			case ACL_USER:
			case ACL_GROUP:
				if (ae.e_stat == ES_DEL)
					break;
			default:
				GOTO(_out, rc = -EIO);
			}
		}
	} else {
		/* maybe there are valid ACL_USER or ACL_GROUP entries in the
		 * original server-side ACL, they are regarded as ES_UNC stat.*/
		int ori_posix_count;

		if (unlikely(size < 0))
			return -EINVAL;
		else if (!size)
			ori_posix_count = 0;
		else
			ori_posix_count =
				CFS_ACL_XATTR_COUNT(size, posix_acl_xattr);
		posix_count = ori_posix_count + ext_count;
		posix_size =
			CFS_ACL_XATTR_SIZE(posix_count, posix_acl_xattr);
		OBD_ALLOC(new, posix_size);
		if (unlikely(new == NULL))
			return -ENOMEM;

		new->a_version = cpu_to_le32(CFS_ACL_XATTR_VERSION);
		/* 1. process the unchanged ACL entries
		 *    in the original server-side ACL. */
		pos = 0;
		for (i = 0, j = 0; i < ori_posix_count; i++) {
			ee = lustre_ext_acl_xattr_search(ext_header,
					&posix_header->a_entries[i], &pos);
			if (ee == NULL)
				memcpy(&new->a_entries[j++],
				       &posix_header->a_entries[i],
				       sizeof(posix_acl_xattr_entry));
		}

		/* 2. process the non-deleted entries
		 *    from client-side extended ACL. */
		for (i = 0; i < ext_count; i++) {
			if (le16_to_cpu(ext_header->a_entries[i].e_stat) !=
			    ES_DEL) {
				new->a_entries[j].e_tag =
						ext_header->a_entries[i].e_tag;
				new->a_entries[j].e_perm =
						ext_header->a_entries[i].e_perm;
				new->a_entries[j++].e_id =
						ext_header->a_entries[i].e_id;
			}
		}
	}

	/* free unused space. */
	rc = lustre_posix_acl_xattr_reduce_space(&new, posix_count, j);
	if (rc >= 0) {
		posix_size = rc;
		*out = new;
		rc = 0;
	}

_out:
	if (rc) {
		OBD_FREE(new, posix_size);
		posix_size = rc;
	}
	return posix_size;
}
EXPORT_SYMBOL(lustre_acl_xattr_merge2posix);

/*
 * Merge the posix ACL and the extended ACL into new extended ACL.
 */
ext_acl_xattr_header *
lustre_acl_xattr_merge2ext(posix_acl_xattr_header *posix_header, int size,
			   ext_acl_xattr_header *ext_header)
{
	int ori_ext_count, posix_count, ext_count, ext_size;
	int i, j, pos = 0, rc = 0;
	posix_acl_xattr_entry pae;
	ext_acl_xattr_header *new;
	ext_acl_xattr_entry *ee, eae;

	if (unlikely(size < 0))
		return ERR_PTR(-EINVAL);
	else if (!size)
		posix_count = 0;
	else
		posix_count = CFS_ACL_XATTR_COUNT(size, posix_acl_xattr);
	ori_ext_count = le32_to_cpu(ext_header->a_count);
	ext_count = posix_count + ori_ext_count;
	ext_size = CFS_ACL_XATTR_SIZE(ext_count, ext_acl_xattr);

	OBD_ALLOC(new, ext_size);
	if (unlikely(new == NULL))
		return ERR_PTR(-ENOMEM);

	for (i = 0, j = 0; i < posix_count; i++) {
		lustre_posix_acl_le_to_cpu(&pae, &posix_header->a_entries[i]);
		switch (pae.e_tag) {
		case ACL_USER_OBJ:
		case ACL_GROUP_OBJ:
		case ACL_MASK:
		case ACL_OTHER:
			if (pae.e_id != ACL_UNDEFINED_ID)
				GOTO(out, rc = -EIO);
		case ACL_USER:
			/* ignore "nobody" entry. */
			if (pae.e_id == NOBODY_UID)
				break;

			new->a_entries[j].e_tag =
					posix_header->a_entries[i].e_tag;
			new->a_entries[j].e_perm =
					posix_header->a_entries[i].e_perm;
			new->a_entries[j].e_id =
					posix_header->a_entries[i].e_id;
			ee = lustre_ext_acl_xattr_search(ext_header,
					&posix_header->a_entries[i], &pos);
			if (ee) {
				if (posix_header->a_entries[i].e_perm !=
								ee->e_perm)
					/* entry modified. */
					ee->e_stat =
					new->a_entries[j++].e_stat =
							cpu_to_le32(ES_MOD);
				else
					/* entry unchanged. */
					ee->e_stat =
					new->a_entries[j++].e_stat =
							cpu_to_le32(ES_UNC);
			} else {
				/* new entry. */
				new->a_entries[j++].e_stat =
							cpu_to_le32(ES_ADD);
			}
			break;
		case ACL_GROUP:
			/* ignore "nobody" entry. */
			if (pae.e_id == NOBODY_GID)
				break;
			new->a_entries[j].e_tag =
					posix_header->a_entries[i].e_tag;
			new->a_entries[j].e_perm =
					posix_header->a_entries[i].e_perm;
			new->a_entries[j].e_id =
					posix_header->a_entries[i].e_id;
			ee = lustre_ext_acl_xattr_search(ext_header,
					&posix_header->a_entries[i], &pos);
			if (ee) {
				if (posix_header->a_entries[i].e_perm !=
								ee->e_perm)
					/* entry modified. */
					ee->e_stat =
					new->a_entries[j++].e_stat =
							cpu_to_le32(ES_MOD);
				else
					/* entry unchanged. */
					ee->e_stat =
					new->a_entries[j++].e_stat =
							cpu_to_le32(ES_UNC);
			} else {
				/* new entry. */
				new->a_entries[j++].e_stat =
							cpu_to_le32(ES_ADD);
			}
			break;
		default:
			GOTO(out, rc = -EIO);
		}
	}

	/* process deleted entries. */
	for (i = 0; i < ori_ext_count; i++) {
		lustre_ext_acl_le_to_cpu(&eae, &ext_header->a_entries[i]);
		if (eae.e_stat == ES_UNK) {
			/* ignore "nobody" entry. */
			if ((eae.e_tag == ACL_USER && eae.e_id == NOBODY_UID) ||
			    (eae.e_tag == ACL_GROUP && eae.e_id == NOBODY_GID))
				continue;

			new->a_entries[j].e_tag =
						ext_header->a_entries[i].e_tag;
			new->a_entries[j].e_perm =
						ext_header->a_entries[i].e_perm;
			new->a_entries[j].e_id = ext_header->a_entries[i].e_id;
			new->a_entries[j++].e_stat = cpu_to_le32(ES_DEL);
		}
	}

	new->a_count = cpu_to_le32(j);
	/* free unused space. */
	rc = lustre_ext_acl_xattr_reduce_space(&new, ext_count);

out:
	if (rc) {
		OBD_FREE(new, ext_size);
		new = ERR_PTR(rc);
	}
	return new;
}
EXPORT_SYMBOL(lustre_acl_xattr_merge2ext);

#endif