Linux – How does Linux tell an LSI Megaraid card to flush its cache before shutdown

linuxmegaraid

The LSI Megaraid controller has a Battery back up for its write cache, which allows, in the event of an unanticipated power outage, to retain the cached, not yet written data, so it can write it out to disk next time the server is booted up. In the event of a clean shutdown, this back up is presumably not necessary, as part of the clean shutdown will involve the controller writing out the cached data to disk.

How does the raid controller get informed that the shutdown is about to happen? Presumably this flush must be the last thing to occur after other OS shutdown code, which might involve writing out data or logs to the raid.

Is there some PCIe level signalling that informs peripherals of a pending power down? Does the Megaraid_sas kernel module inform the card of the pending shutdown? Or does a MegaCli command to flush the cache need to be run from an init script for the shutdown?

I've manually added a Megaraid card and additional set of drives to a Linux system which was installed without one, and want to be sure that I don't need to manually add something in to guarantee the cache is consistent on shutdown.

Best Answer

During shutdown the system will unmount most of the filesystems. Those that can not be unmounted are remounted read-only. Umount syscall will flush the disk caches via VFS, and disk buffers using the driver behind the block device. The block device driver in your case is the generic SCSI driver sg. This will send the data to the SCSI device using the specific driver for your device and a SCSI flush command so that the data is written from internal disk cache to the disk storage medium. All of this is synchronous and the umount will not return until the data is on the disk. For remounted read-only FS the shutdown scripts will just run the sync syscall.