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
/*
 * linux/fs/hfs/inode.c
 *
 * Copyright (C) 1995-1997  Paul H. Hargrove
 * This file may be distributed under the terms of the GNU Public License.
 *
 * This file contains inode-related functions which do not depend on
 * which scheme is being used to represent forks.
 *
 * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
 *
 * "XXX" in a comment is a note to myself to consider changing something.
 *
 * In function preconditions the term "valid" applied to a pointer to
 * a structure means that the pointer is non-NULL and the structure it
 * points to has all fields initialized to consistent values.
 */

#include "hfs.h"
#include <linux/hfs_fs_sb.h>
#include <linux/hfs_fs_i.h>
#include <linux/hfs_fs.h>

/*================ Variable-like macros ================*/

#define HFS_VALID_MODE_BITS  (S_IFREG | S_IFDIR | S_IRWXUGO)

/*================ File-local functions ================*/

/*
 * init_file_inode()
 *
 * Given an HFS catalog entry initialize an inode for a file.
 */
static void init_file_inode(struct inode *inode, hfs_u8 fork)
{
	struct hfs_fork *fk;
	struct hfs_cat_entry *entry = HFS_I(inode)->entry;

	if (!IS_NOEXEC(inode) && (fork == HFS_FK_DATA)) {
		inode->i_mode = S_IRWXUGO | S_IFREG;
	} else {
		inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
	}

	if (fork == HFS_FK_DATA) {
		hfs_u32 type = hfs_get_nl(entry->info.file.finfo.fdType);

		fk = &entry->u.file.data_fork;
		HFS_I(inode)->convert =
			((HFS_SB(inode->i_sb)->s_conv == 't') ||
			 ((HFS_SB(inode->i_sb)->s_conv == 'a') &&
			  ((type == htonl(0x54455854)) ||   /* "TEXT" */
			   (type == htonl(0x7474726f)))));  /* "ttro" */
	} else {
		fk = &entry->u.file.rsrc_fork;
		HFS_I(inode)->convert = 0;
	}
	HFS_I(inode)->fork = fk;
	inode->i_size = fk->lsize;
	inode->i_blocks = fk->psize;
	inode->i_nlink = 1;
}

/*================ Global functions ================*/

/*
 * hfs_put_inode()
 *
 * This is the put_inode() entry in the super_operations for HFS
 * filesystems.  The purpose is to perform any filesystem-dependent 
 * cleanup necessary when the use-count of an inode falls to zero.
 */
void hfs_put_inode(struct inode * inode)
{
	struct hfs_cat_entry *entry = HFS_I(inode)->entry;

	hfs_cat_put(entry);
	if (inode->i_count == 1) {
	  struct hfs_hdr_layout *tmp = HFS_I(inode)->layout;

	  if (tmp) {
		HFS_I(inode)->layout = NULL;
		HFS_DELETE(tmp);
	  }
	}
}

/*
 * hfs_notify_change()
 *
 * Based very closely on fs/msdos/inode.c by Werner Almesberger
 *
 * This is the notify_change() field in the super_operations structure
 * for HFS file systems.  The purpose is to take that changes made to
 * an inode and apply then in a filesystem-dependent manner.  In this
 * case the process has a few of tasks to do:
 *  1) prevent changes to the i_uid and i_gid fields.
 *  2) map file permissions to the closest allowable permissions
 *  3) Since multiple Linux files can share the same on-disk inode under
 *     HFS (for instance the data and resource forks of a file) a change
 *     to permissions must be applied to all other in-core inodes which 
 *     correspond to the same HFS file.
 */
int hfs_notify_change(struct dentry *dentry, struct iattr * attr)
{
	struct inode *inode = dentry->d_inode;
	struct hfs_cat_entry *entry = HFS_I(inode)->entry;
	struct dentry **de = entry->sys_entry;
	struct hfs_sb_info *hsb = HFS_SB(inode->i_sb);
	int error, i;

	error = inode_change_ok(inode, attr); /* basic permission checks */
	if (error) {
		/* Let netatalk's afpd think chmod() always succeeds */
		if (hsb->s_afpd &&
		    (attr->ia_valid == (ATTR_MODE | ATTR_CTIME))) {
			return 0;
		} else {
			return error;
		}
	}

	/* no uig/gid changes and limit which mode bits can be set */
	if (((attr->ia_valid & ATTR_UID) && 
	     (attr->ia_uid != hsb->s_uid)) ||
	    ((attr->ia_valid & ATTR_GID) && 
	     (attr->ia_gid != hsb->s_gid)) ||
	    ((attr->ia_valid & ATTR_MODE) &&
	     (((entry->type == HFS_CDR_DIR) &&
	       (attr->ia_mode != inode->i_mode))||
	      (attr->ia_mode & ~HFS_VALID_MODE_BITS)))) {
		return hsb->s_quiet ? 0 : error;
	}
	
	if (entry->type == HFS_CDR_DIR) {
		attr->ia_valid &= ~ATTR_MODE;
	} else if (attr->ia_valid & ATTR_MODE) {
		/* Only the 'w' bits can ever change and only all together. */
		if (attr->ia_mode & S_IWUSR) {
			attr->ia_mode = inode->i_mode | S_IWUGO;
		} else {
			attr->ia_mode = inode->i_mode & ~S_IWUGO;
		}
		attr->ia_mode &= ~hsb->s_umask;
	}
	inode_setattr(inode, attr);

	/* We wouldn't want to mess with the sizes of the other fork */
	attr->ia_valid &= ~ATTR_SIZE;

	/* We must change all in-core inodes corresponding to this file. */
	for (i = 0; i < 4; ++i) {
	  if (de[i] && (de[i] != dentry)) {
		inode_setattr(de[i]->d_inode, attr);
	  }
	}

	/* Change the catalog entry if needed */
	if (attr->ia_valid & ATTR_MTIME) {
		entry->modify_date = hfs_u_to_mtime(inode->i_mtime);
		hfs_cat_mark_dirty(entry);
	}
	if (attr->ia_valid & ATTR_MODE) {
		hfs_u8 new_flags;

		if (inode->i_mode & S_IWUSR) {
			new_flags = entry->u.file.flags & ~HFS_FIL_LOCK;
		} else {
			new_flags = entry->u.file.flags | HFS_FIL_LOCK;
		}

		if (new_flags != entry->u.file.flags) {
			entry->u.file.flags = new_flags;
			hfs_cat_mark_dirty(entry);
		}
	}
	/* size changes handled in hfs_extent_adj() */

	return 0;
}

/*
 * __hfs_iget()
 *
 * Given the MDB for a HFS filesystem, a 'key' and an 'entry' in
 * the catalog B-tree and the 'type' of the desired file return the
 * inode for that file/directory or NULL.  Note that 'type' indicates
 * whether we want the actual file or directory, or the corresponding
 * metadata (AppleDouble header file or CAP metadata file).
 *
 * In an ideal world we could call iget() and would not need this
 * function.  However, since there is no way to even know the inode
 * number until we've found the file/directory in the catalog B-tree
 * that simply won't happen.
 *
 * The main idea here is to look in the catalog B-tree to get the
 * vital info about the file or directory (including the file id which
 * becomes the inode number) and then to call iget() and return the
 * inode if it is complete.  If it is not then we use the catalog
 * entry to fill in the missing info, by calling the appropriate
 * 'fillin' function.  Note that these fillin functions are
 * essentially hfs_*_read_inode() functions, but since there is no way
 * to pass the catalog entry through iget() to such a read_inode()
 * function, we have to call them after iget() returns an incomplete
 * inode to us.	 This is pretty much the same problem faced in the NFS
 * code, and pretty much the same solution. The SMB filesystem deals
 * with this in a different way: by using the address of the
 * kmalloc()'d space which holds the data as the inode number.
 *
 * XXX: Both this function and NFS's corresponding nfs_fhget() would
 * benefit from a way to pass an additional (void *) through iget() to
 * the VFS read_inode() function.
 *
 * this will hfs_cat_put() the entry if it fails.
 */
struct inode *hfs_iget(struct hfs_cat_entry *entry, ino_t type,
		       struct dentry *dentry)
{
	struct dentry **sys_entry;
	struct super_block *sb;
	struct inode *inode;

	if (!entry) {
		return NULL;
	}

	/* If there are several processes all calling __iget() for
	   the same inode then they will all get the same one back.
	   The first one to return from __iget() will notice that the
	   i_mode field of the inode is blank and KNOW that it is
	   the first to return.  Therefore, it will set the appropriate
	   'sys_entry' field in the entry and initialize the inode.
	   All the initialization must be done without sleeping,
	   or else other processes could end up using a partially
	   initialized inode.				*/

	sb = entry->mdb->sys_mdb;
	sys_entry = &entry->sys_entry[HFS_ITYPE_TO_INT(type)];

	if (!(inode = iget(sb, ntohl(entry->cnid) | type))) {
	        hfs_cat_put(entry);
	        return NULL;
	}

	if (inode->i_dev != sb->s_dev) {
	        iput(inode); /* automatically does an hfs_cat_put */
		inode = NULL;
	} else if (!inode->i_mode || (*sys_entry == NULL)) {
		/* Initialize the inode */
		struct hfs_sb_info *hsb = HFS_SB(sb);

		inode->i_rdev = 0;
		inode->i_ctime = inode->i_atime = inode->i_mtime =
					hfs_m_to_utime(entry->modify_date);
		inode->i_blksize = HFS_SECTOR_SIZE;
		inode->i_uid = hsb->s_uid;
		inode->i_gid = hsb->s_gid;

		memset(HFS_I(inode), 0, sizeof(struct hfs_inode_info));
		HFS_I(inode)->magic = HFS_INO_MAGIC;
		HFS_I(inode)->entry = entry;
		HFS_I(inode)->tz_secondswest = hfs_to_utc(0);

		hsb->s_ifill(inode, type);
		if (!hsb->s_afpd && (entry->type == HFS_CDR_FIL) &&
		    (entry->u.file.flags & HFS_FIL_LOCK)) {
			inode->i_mode &= ~S_IWUGO;
		}
		inode->i_mode &= ~hsb->s_umask;

		if (!inode->i_mode) {
			iput(inode); /* does an hfs_cat_put */
			inode = NULL;
		} else
			*sys_entry = dentry; /* cache dentry */

	}

	return inode;
}

/*================ Scheme-specific functions ================*/

/* 
 * hfs_cap_ifill()
 *
 * This function serves the same purpose as a read_inode() function does
 * in other filesystems.  It is called by __hfs_iget() to fill in
 * the missing fields of an uninitialized inode under the CAP scheme.
 */
void hfs_cap_ifill(struct inode * inode, ino_t type)
{
	struct hfs_cat_entry *entry = HFS_I(inode)->entry;

	HFS_I(inode)->d_drop_op = hfs_cap_drop_dentry;
	if (type == HFS_CAP_FNDR) {
		inode->i_size = sizeof(struct hfs_cap_info);
		inode->i_blocks = 0;
		inode->i_nlink = 1;
		inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
		inode->i_op = &hfs_cap_info_inode_operations;
	} else if (entry->type == HFS_CDR_FIL) {
		init_file_inode(inode, (type == HFS_CAP_DATA) ?
						HFS_FK_DATA : HFS_FK_RSRC);
		inode->i_op = &hfs_file_inode_operations;
	} else { /* Directory */
		struct hfs_dir *hdir = &entry->u.dir;

		inode->i_blocks = 0;
		inode->i_size = hdir->files + hdir->dirs + 5;
		HFS_I(inode)->dir_size = 1;
		if (type == HFS_CAP_NDIR) {
			inode->i_mode = S_IRWXUGO | S_IFDIR;
			inode->i_nlink = hdir->dirs + 4;
			inode->i_op = &hfs_cap_ndir_inode_operations;
			HFS_I(inode)->file_type = HFS_CAP_NORM;
		} else if (type == HFS_CAP_FDIR) {
			inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
			inode->i_nlink = 2;
			inode->i_op = &hfs_cap_fdir_inode_operations;
			HFS_I(inode)->file_type = HFS_CAP_FNDR;
		} else if (type == HFS_CAP_RDIR) {
			inode->i_mode = S_IRUGO | S_IXUGO | S_IFDIR;
			inode->i_nlink = 2;
			inode->i_op = &hfs_cap_rdir_inode_operations;
			HFS_I(inode)->file_type = HFS_CAP_RSRC;
		}
	}
}

/* 
 * hfs_dbl_ifill()
 *
 * This function serves the same purpose as a read_inode() function does
 * in other filesystems.  It is called by __hfs_iget() to fill in
 * the missing fields of an uninitialized inode under the AppleDouble
 * scheme.
 */
void hfs_dbl_ifill(struct inode * inode, ino_t type)
{
	struct hfs_cat_entry *entry = HFS_I(inode)->entry;

	HFS_I(inode)->d_drop_op = hfs_dbl_drop_dentry;
	if (type == HFS_DBL_HDR) {
		if (entry->type == HFS_CDR_FIL) {
			init_file_inode(inode, HFS_FK_RSRC);
			inode->i_size += HFS_DBL_HDR_LEN;
			HFS_I(inode)->default_layout = &hfs_dbl_fil_hdr_layout;
		} else {
			inode->i_size = HFS_DBL_HDR_LEN;
			inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
			inode->i_nlink = 1;
			HFS_I(inode)->default_layout = &hfs_dbl_dir_hdr_layout;
		}
		inode->i_op = &hfs_hdr_inode_operations;
	} else if (entry->type == HFS_CDR_FIL) {
		init_file_inode(inode, HFS_FK_DATA);
		inode->i_op = &hfs_file_inode_operations;
	} else { /* Directory */
		struct hfs_dir *hdir = &entry->u.dir;

		inode->i_blocks = 0;
		inode->i_nlink = hdir->dirs + 2;
		inode->i_size = 3 + 2 * (hdir->dirs + hdir->files);
		inode->i_mode = S_IRWXUGO | S_IFDIR;
		inode->i_op = &hfs_dbl_dir_inode_operations;
		HFS_I(inode)->file_type = HFS_DBL_NORM;
		HFS_I(inode)->dir_size = 2;
	}
}

/* 
 * hfs_nat_ifill()
 *
 * This function serves the same purpose as a read_inode() function does
 * in other filesystems.  It is called by __hfs_iget() to fill in
 * the missing fields of an uninitialized inode under the Netatalk
 * scheme.
 */
void hfs_nat_ifill(struct inode * inode, ino_t type)
{
	struct hfs_cat_entry *entry = HFS_I(inode)->entry;

	HFS_I(inode)->d_drop_op = hfs_nat_drop_dentry;
	if (type == HFS_NAT_HDR) {
		if (entry->type == HFS_CDR_FIL) {
			init_file_inode(inode, HFS_FK_RSRC);
			inode->i_size += HFS_NAT_HDR_LEN;
		} else {
			inode->i_size = HFS_NAT_HDR_LEN;
			inode->i_mode = S_IRUGO | S_IWUGO | S_IFREG;
			inode->i_nlink = 1;
		}
		inode->i_op = &hfs_hdr_inode_operations;
		HFS_I(inode)->default_layout = &hfs_nat_hdr_layout;
	} else if (entry->type == HFS_CDR_FIL) {
		init_file_inode(inode, HFS_FK_DATA);
		inode->i_op = &hfs_file_inode_operations;
	} else { /* Directory */
		struct hfs_dir *hdir = &entry->u.dir;

		inode->i_blocks = 0;
		inode->i_size = hdir->files + hdir->dirs + 4;
		inode->i_mode = S_IRWXUGO | S_IFDIR;
		HFS_I(inode)->dir_size = 1;
		if (type == HFS_NAT_NDIR) {
			inode->i_nlink = hdir->dirs + 3;
			inode->i_op = &hfs_nat_ndir_inode_operations;
			HFS_I(inode)->file_type = HFS_NAT_NORM;
		} else if (type == HFS_NAT_HDIR) {
			inode->i_nlink = 2;
			inode->i_op = &hfs_nat_hdir_inode_operations;
			HFS_I(inode)->file_type = HFS_NAT_HDR;
		}
	}
}