(1)For unlink and close, I was totally misunderstood. Since in the program,the unlink
is before close,so just the name of the file is removed and the data blocks,inode
are still in the disk,when close,iput,ifree and free decrease rc,release data block
and inode,also put data block into superblock free list when rc=0,ln=0,right?


put data block into superblock free list when ONLY when rc=0,ln=0,

if the file is still open rc>0


(2)I don't understand iget:
we have ialloc to assign a disk inode to the file,to open the file,do we need to assgin
the inode inside memory free list,or just a copy?
if the inode free list is empty the kernel returns an error:how can i understand?


ialloc allocate a new inode to a file, it remain assigned until the file exists)

iget is used by open of an existing file

if the inode free list is empty the kernel returns an error  YES because the files of this filesystem

possbly afe never removed.


(3) since chdir and chroot won't lead unmount or mount,how can the filesystem
to be reorganized?

chdir and chroot make reference to existing directories, i.e. directories that already exist in the current filesystem tree 

(4)for the alloc,I think when the file is written,the file will create part or many
data blocks,Data block is sequence of bytes,right? I also think just to allocate block
number to specify the block created is ok,why the file needs to use alloc to allocate

alloc is needed because the file grows. After filling the first block you need another block to continue writing new data.

When the file size grows beyond the size of 10 * block_size, the kernel need to allocate a new block and also an indirect index block  

(1) br------ 2 root root 0,21 Feb 12 15:40 /dev/dsk15
crw-rw-- 2 root root 7,21 Mar 7 09:29 /dev/rdsk15
because the two special files propably refer to same partition,so
they refer to to same device,they are just two files of same
which stands for different access method,block and character,righ
If right,does it mean there are some devices can be accessed by
block and character methods,but when to use block,when to use

SAME device accessed through buffer cache (c) or raw (r)

In program Blockraw.c,two different file descriptors with same
offset for lseek,
what should i infer from it?(I don't test the program because I
don't find the
files using "ls")

Will print "Buffers are identicaln");

(2)In Buffer cache,to minimize the number of disk accesses,it has
delayed write method,
it is to avoid the read of the block on disk,that is,after
transferring the content to disk,
there may be modifying on the block in buffer cache again,so it's

better to delay, then transferring the changes together later,right?

OK, keep as much as possible the buffer in memory

(3)getblk is for allocating the buffer from the buffer pool to a
block,in breada procedure,
after getblk,why the data in the buffer can be invalid? Is it due
to getblk doesn't put block
into buffer,just assign the buffer to it?

The buffer is invalid because the data are not been completely

On 01.05.2016 18:53, Pietro Laface wrote:

The offset is the same for the two special files
The access method does not change the content of the blocks that are
using the character device read the transfer goes directly to the user
space, whereas
using the block device there is a double copy (to the buffer cache,
and from this buffer ti the user buffer).

(1)Today I did lab7, when I checked the result of the special files using:
cat chardev_SDP_lab,I found it was the characters I input from keyboard.

Could I say character or block device(which are special files) store data
read from input to the device;or the data is in fact inside kernel memory,
but accessed through special files?

A character driver reads from a device, through the special file, in its kernel buffer, which is then copied to the user buffer. 

(2)Since some devices like hard disk can have both block and character device
for two access methods,how to decide which method to access?



The management of block device driver is more complex, and it  has not been addressed.

A block device is used only for devices such as disks, where the minimum amount of transfer

is a block.

Raw access is typically performed by system programs such as mkfs of fsck, which of course

need to create or restore a file system

mkfs - build a Linux file system

(mkfs is used to build a Linux file system on a device, usually a hard disk partition. filesys is either the device name (e.g. /dev/hda1/dev/sdb2), or a regular file that shall contain the file system. blocks is the number of blocks to be used for the file system.

fsck - check and repair a Linux file system

fsck is used to check and optionally repair one or more Linux file systems. filesys can be a device name (e.g./dev/hdc1/dev/sdb2), a mount point (e.g. //usr,/home), or an ext2 label or UUID specifier (e.g. UUID=8868abf6-88c5-4a83-98b8-bfc24057f7bd or LABEL=root). Normally, the fsck program will try to handle filesystems on different physical disk drives in parallel to reduce the total amount of time needed to check all of the filesystems.

(3)for 7.2,I modified code as following:
device_read(struct file *filp, char *buff, size_t count, loff_t *offp) {
unsigned long ret;
  int ss=strlen(char_dev_buf);
  printk(KERN_INFO "the char_dev_buf is : %d\n", strlen(char_dev_buf));
        ret = copy_to_user(buff, char_dev_buf, ss);
        return ss;

I just copy precise length of data in kernel buffer to user buffer,it works,
but I hesitate to clear or not,if not,it can have unclear output which mixes
input from different times;if yes,I think it won't work in real case because
it looks like just read once,then delete.Could you please give me some suggestions?

It's OK because you just want to pass to the user the input line up to the '\n'. 

(4)I create a block device after character device works for module chardev_SDP_lab.ko,

 laface@VMdebian6:/var/log$ ls -l /dev/chardev_SDP_lab*
crw-r--r-- 1 root root 251, 0 May  6 18:15 /dev/chardev_SDP_lab
brw-r--r-- 1 root root 251, 0 May  6 21:42 /dev/chardev_SDP_lab_block

but when I tried to write into it,failed:

root@VMdebian6:/home/laface/Desktop/lab7/chardev_SDP_lab# echo something > /dev/chardev_SDP_lab_block
bash: /dev/chardev_SDP_lab_block: No such device or address

Does this mean,I can create block and character device freely,but for certain device,it can only work
with character device or block device?

See the second answer 


So for drivers to access device,need to use special files,

In Unix-like operating systems, a device file or special file is an interface for a device driver that appears in a file system as if it were an ordinary file. There are also special files in MS-DOSOS/2, and Microsoft Windows. They allow software to interact with a device driver using standard input/output system calls, which simplifies many tasks and unifies user-space I/O mechanisms.

Device files often provide simple interfaces to peripheral devices, such as printers and serial ports. But they can also be used to access specific resources on those devices, such as disk partitions. Finally, device files are useful for accessing system resources that have no connection with any actual device such asdata sinks and random number generators.

  • A Character ('c') Device is one with which the Driver communicates by sending and receiving single characters (bytes, octets).

  • A Block ('b') Device is one with which the Driver communicates by sending entire blocks of data.

  • Examples for Character Devices: serial ports, parallel ports, sounds cards.

  • Examples for Block Devices: hard disks, USB cameras, Disk-On-Key.

  • so in the slides,i infer that both character and block devices refer to same device(cz same partition),just being accessed through buffer or not.And special file is just an interface of a device driver appeared in a file system.