container_of マクロ

昨晩の get_gendisk() 手続き定義の直上に to_disk なマクロの定義があった。

#define to_disk(obj) container_of(obj,struct gendisk,kobj)

/**
 * get_gendisk - get partitioning information for a given device
 * @dev: device to get partitioning information for
 *
 * This function gets the structure containing partitioning
 * information for the given device @dev.
 */
struct gendisk *get_gendisk(dev_t dev, int *part)
{
	struct kobject *kobj = kobj_lookup(bdev_map, dev, part);
	return  kobj ? to_disk(kobj) : NULL;
}

むむ。container_of マクロですか。ググッたら自分のエントリが出てきた。

super class を求める云々な記述あり。詳細は自分のエントリなのに意味不明。とりあえず kobjectl なオブジェクトから super class の gendisk なポインタを求める事ができる、という理解。ちなみに struct gendisk 型の定義は以下 (include/linux/genhd.h)。

struct gendisk {
	int major;			/* major number of driver */
	int first_minor;
	int minors;                     /* maximum number of minors, =1 for
                                         * disks that can't be partitioned. */
	char disk_name[32];		/* name of major driver */
	struct hd_struct **part;	/* [indexed by minor] */
	int part_uevent_suppress;
	struct block_device_operations *fops;
	struct request_queue *queue;
	void *private_data;
	sector_t capacity;

	int flags;
	struct device *driverfs_dev;
	struct kobject kobj;
	struct kobject *holder_dir;
	struct kobject *slave_dir;

	struct timer_rand_state *random;
	int policy;

	atomic_t sync_io;		/* RAID */
	unsigned long stamp;
	int in_flight;
#ifdef	CONFIG_SMP
	struct disk_stats *dkstats;
#else
	struct disk_stats dkstats;
#endif
};

確かに struct kobject なポインタの kobj というメンバが居るな。上記のナニで言えば、

  • kobj_lookup() で kobject なポインタを取得
  • それを持ってる struct gendisk なオブジェクトを戻す

という事なんスか。しかし gendisk 構造体って何なんでしょ。

とゆーコトで

今日は早めに休みます。