Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

C++ function call for manual SSD-TRIM commands

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Piglou
Posts: 14
Joined: 2015-07-25 11:55

C++ function call for manual SSD-TRIM commands

#1 Post by Piglou »

Hi everybody,

I'm looking for a way to launch "TRIM" commands to a SSD drive myself, from a c++ code I'm going to write, for flash erasing an SSD drive (and not to wait for others things that "should do it automatically in some circumstances if this or this, this and this have been enabled and [..]" but will never tell me if it worked or not :D

I know there is thousands of complicated ways to test and check if it worked, and also software that needs money to do so, that's why I just want to call myself the TRIM functions and read the return value (like true or false) in order to know if it worked !

Does anybody know where I can find the c++ call that could permit me to do so ?
I heard about the GLibC that gives a way to access every user space function related to Linux Kernel (poll, select and others) as standard c++ functions, I suppose that, if there is a way, it will be on the GLibC but how to find it ?

I'm c++ programmer (HMI and network communication with others devices and machines) but with MSDN for Windows, Qt for Windows and Linux, I miss some of the "pure linux" c++ knowledge (may be just a website that could list me all the available functions !)

Thank you in advance :wink:

Piglou
Posts: 14
Joined: 2015-07-25 11:55

Re: C++ function call for manual SSD-TRIM commands

#2 Post by Piglou »

I began to search about GLibC which is GNU C Library :

http://www.gnu.org/software/libc/manual/
But searching for TRIM did not give me what I was looking for !

Another thing here :
https://ata.wiki.kernel.org/index.php/ATA_Secure_Erase

By looking at hdparm source code may be I have a chance to find how !

User avatar
dilberts_left_nut
Administrator
Administrator
Posts: 5346
Joined: 2009-10-05 07:54
Location: enzed
Has thanked: 12 times
Been thanked: 66 times

Re: C++ function call for manual SSD-TRIM commands

#3 Post by dilberts_left_nut »

Maybe the source of 'fstrim' from util-linux package could help.
AdrianTM wrote:There's no hacker in my grandma...

Piglou
Posts: 14
Joined: 2015-07-25 11:55

Re: C++ function call for manual SSD-TRIM commands

#4 Post by Piglou »

I wasn't aware of the existence of this command, thank you !

RETURN CODES
0 success
1 failure
32 all failed
64 some filesystem discards have succeeded, some failed

Here it can be used on a mounted file system so it's interesting too. I will also look after the ATA_Secure_Erase in order to see if it's the same commands that is used or not

EDIT : here is an interesting part of the fstrim source code, because it's a simple part - it seems that ioctl and device driver at /dev/sdx are able to do this easily
I will do some test on my SSD in order to be sure that I really ask to these commands what I need ! All my datas are on several computer using "Sync" so I can't lost anything, may be some cookies :mrgreen:

Code: Select all

/* returns: 0 = success, 1 = unsupported, < 0 = error */
static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl, int verbose)
{
	int fd;
	struct stat sb;
	struct fstrim_range range;

	/* kernel modifies the range */
	memcpy(&range, rangetpl, sizeof(range));

	fd = open(path, O_RDONLY);
	if (fd < 0) {
		warn(_("cannot open %s"), path);
		return -1;
	}
	if (fstat(fd, &sb) == -1) {
		warn(_("stat failed %s"), path);
		return -1;
	}
	if (!S_ISDIR(sb.st_mode)) {
		warnx(_("%s: not a directory"), path);
		return -1;
	}
	errno = 0;
	if (ioctl(fd, FITRIM, &range)) {
		int rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -1;

		if (rc != 1)
			warn(_("%s: FITRIM ioctl failed"), path);
		close(fd);
		return rc;
	}

	if (verbose) {
		char *str = size_to_human_string(
				SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE,
				(uint64_t) range.len);
		/* TRANSLATORS: The standard value here is a very large number. */
		printf(_("%s: %s (%" PRIu64 " bytes) trimmed\n"),
				path, str, (uint64_t) range.len);
		free(str);
	}
	close(fd);
	return 0;
}
ioctl(fd, FITRIM, &range)

* This program uses FITRIM ioctl to discard parts or the whole filesystem
* online (mounted). You can specify range (start and length) to be
* discarded, or simply discard whole filesystem.


When I have a working application using all this, I come back to you !
I will try to design something cool with Qt.

Post Reply