There isn't a way to do this totally on the 9P. You would have to have some kind of hardware solution working with the 9P. For example, a circut that would detect when the power stopped (from the wall), perhaps you'd have some kind of small battery or super capacitor that would keep the 9P alive. At the same time you would send a signal on the 9Ps GPIO lines, changing the line state. The 9P would detect this change and then save the critical data to NVRAM or whever it needed to.