All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Sterba <dave@jikos.cz>
To: dima <dolenin@parallels.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: Set nodatacow per file?
Date: Mon, 13 Feb 2012 15:10:40 +0100	[thread overview]
Message-ID: <20120213141040.GG15350@twin.jikos.cz> (raw)
In-Reply-To: <4F38BE53.4080204@parallels.com>

[-- Attachment #1: Type: text/plain, Size: 548 bytes --]

Hi,

On Mon, Feb 13, 2012 at 04:40:03PM +0900, dima wrote:
> Actually it is possible. Check out David's response to my question from 
> some time ago:
> http://permalink.gmane.org/gmane.comp.file-systems.btrfs/14227

this was a quick aid, please see attached file for an updated tool to set
the file flags, now added 'z' for NOCOMPRESS flag, and supports chattr
syntax plus all of the standard file flags.

Setting and unsetting nocow is done like 'fileflags +C file' or -C for
unseting. Without any + or - options it prints current state.


david

[-- Attachment #2: fileflags.c --]
[-- Type: text/plain, Size: 3201 bytes --]

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>

#ifndef FS_NOCOMP_FL
#define FS_NOCOMP_FL                    0x00000400 /* Don't compress */
#endif

#ifndef FS_NOCOW_FL
#define FS_NOCOW_FL                     0x00800000 /* Do not cow file */
#endif

struct flags_name {
	unsigned long	flag;
	const char	short_name;
	const char	*long_name;
};

static const struct flags_name flags[]={
	/* new */
	{ FS_NOCOW_FL, 'C', "NOCOW" },
	{ FS_NOCOMP_FL, 'z', "Not_Compressed" },
	/* current */
	{ FS_SECRM_FL, 's', "Secure_Deletion" },
	{ FS_UNRM_FL, 'u' , "Undelete" },
	{ FS_SYNC_FL, 'S', "Synchronous_Updates" },
	{ FS_DIRSYNC_FL, 'D', "Synchronous_Directory_Updates" },
	{ FS_IMMUTABLE_FL, 'i', "Immutable" },
	{ FS_APPEND_FL, 'a', "Append_Only" },
	{ FS_NODUMP_FL, 'd', "No_Dump" },
	{ FS_NOATIME_FL, 'A', "No_Atime" },
	{ FS_COMPR_FL, 'c', "Compression_Requested" },
	{ FS_COMPRBLK_FL, 'B', "Compressed_File" },
	{ FS_DIRTY_FL, 'Z', "Compressed_Dirty_File" },
	{ FS_ECOMPR_FL, 'E', "Compression_Error" },
	{ FS_JOURNAL_DATA_FL, 'j', "Journaled_Data" },
	{ FS_INDEX_FL, 'I', "Indexed_directory" },
	{ FS_NOTAIL_FL, 't', "No_Tailmerging" },
	{ FS_TOPDIR_FL, 'T', "Top_of_Directory_Hierarchies" },
	/* { EXT4_EXTENTS_FL, 'e', "Extents" }, */
	/* { EXT4_HUGE_FILE_FL, 'h', "Huge_file" }, */
};

static unsigned long to_set, to_unset;

unsigned long c2val(char c)
{
	int i;

	for(i=0;i<sizeof(flags)/sizeof(flags[0]);i++) {
		if(flags[i].short_name==c)
			return flags[i].flag;
	}

	printf("Warning: flag '%c' not found\n", c);
	return 0;
}
void list_flags(unsigned long fflags)
{
	int i;

	printf("Flags:\n");
	for(i=0;i<sizeof(flags)/sizeof(flags[0]);i++) {
		if(fflags & flags[i].flag) {
			printf(" %c %s\n",
				flags[i].short_name,
				flags[i].long_name);
		}
	}
}

void set_flag(const char* in)
{
	int i;

	for(i=0;i<strlen(in);i++) {
		to_set |= c2val(in[i]);
		printf(" set %c\n", in[i]);
	}
}

void unset_flag(const char* in)
{
	int i;

	for(i=0;i<strlen(in);i++) {
		to_unset |= c2val(in[i]);
		printf(" unset %c\n", in[i]);
	}
}

int main(int argc, char **argv)
{
        int ret;
	int optind;
	int modify=0;

	if (argc < 2) {
		printf("usage: %s [options] [--] file...\n", argv[0]);
		exit(1);
	}
	to_set = 0;
	to_unset = 0;
	for(optind=1;optind<argc;optind++) {
		char *o=argv[optind];
		if(o[0]=='-' && o[1]=='-') {
			optind++;
			break;
		} else if(o[0]=='-') {
			unset_flag(o+1);
			modify=1;
		} else if(o[0]=='+') {
			set_flag(o+1);
			modify=1;
		} else break;
	}

	for(;optind<argc;optind++) {
		unsigned long fflags;
		int fd = -1;

		if(fd!=-1) close(fd);
		fd = open(argv[optind], O_RDONLY);
		if (fd == -1) {
			perror("open()");
			continue;
		}
		ret = ioctl(fd, FS_IOC_GETFLAGS, &fflags);
		if (ret == -1) {
			perror("ioctl(GETFLAGS)");
			continue;
		}
		if(modify) {
			fflags |= to_set;
			fflags &= ~to_unset;
			ret = ioctl(fd, FS_IOC_SETFLAGS, &fflags);
			if (ret == -1) {
				perror("ioctl(SETFLAGS)");
				continue;
			}
		}
		printf("File: %s\n", argv[optind]);
		list_flags(fflags);
		putchar('\n');
	}

        return 0;
}

  parent reply	other threads:[~2012-02-13 14:10 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-13  7:17 Set nodatacow per file? Ralf-Peter Rohbeck
2012-02-13  7:34 ` Chester
2012-02-13  8:10   ` Liu Bo
2012-02-13  7:40 ` dima
2012-02-13  8:09   ` Roman Mamedov
2012-02-13  8:40     ` dima
2012-02-13 13:42     ` dima
2012-02-13 13:51       ` Roman Mamedov
2012-02-13 14:31         ` Dmitry Olenin
2012-02-13 14:10   ` David Sterba [this message]
2012-02-13 14:21     ` Timo Witte
2012-02-13 15:10     ` dima
2012-02-16 13:55     ` NOCOW + compress-force = bug Roman Mamedov
2012-02-16 14:30       ` David Sterba
2012-02-16 17:58       ` Chris Mason
2012-03-13 18:11         ` Jeff Mahoney
2012-03-13 18:36           ` Jeff Mahoney
2012-02-29 15:09     ` Set nodatacow per file? Kyle Gates
2012-02-29 15:34       ` cwillu
2012-02-24  5:22 ` dima
2012-02-27 13:54   ` dima
2012-02-27 22:10     ` Chester
2012-02-28  0:51       ` dima
2012-03-02  3:28         ` dima

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120213141040.GG15350@twin.jikos.cz \
    --to=dave@jikos.cz \
    --cc=dolenin@parallels.com \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.