| 
									
										
										
										
											2016-04-30 08:52:17 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 	"sync" | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | 	"syscall" | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2016-04-30 08:52:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | const ( | 
					
						
							|  |  |  | 	// Block size used for all internal operations version 1.
 | 
					
						
							|  |  |  | 	blockSizeV1 = 10 * 1024 * 1024 // 10MiB.
 | 
					
						
							| 
									
										
										
										
											2016-06-25 18:03:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Staging buffer read size for all internal operations version 1.
 | 
					
						
							|  |  |  | 	readSizeV1 = 128 * 1024 // 128KiB.
 | 
					
						
							| 
									
										
										
										
											2016-05-30 06:38:14 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-16 11:20:38 +08:00
										 |  |  | // Register callback functions that needs to be called when process shutsdown.
 | 
					
						
							|  |  |  | // For now, SIGINT triggers the callbacks, in future controller can trigger
 | 
					
						
							|  |  |  | // shutdown callbacks.
 | 
					
						
							|  |  |  | func registerShutdown(callback func()) { | 
					
						
							|  |  |  | 	go func() { | 
					
						
							|  |  |  | 		trapCh := signalTrap(os.Interrupt, syscall.SIGTERM) | 
					
						
							|  |  |  | 		<-trapCh | 
					
						
							|  |  |  | 		callback() | 
					
						
							|  |  |  | 	}() | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-31 07:51:59 +08:00
										 |  |  | // House keeping code needed for FS.
 | 
					
						
							|  |  |  | func fsHouseKeeping(storageDisk StorageAPI) error { | 
					
						
							|  |  |  | 	// Attempt to create `.minio`.
 | 
					
						
							|  |  |  | 	err := storageDisk.MakeVol(minioMetaBucket) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2016-06-09 13:02:10 +08:00
										 |  |  | 		if err != errVolumeExists && err != errDiskNotFound && err != errFaultyDisk { | 
					
						
							| 
									
										
										
										
											2016-05-31 07:51:59 +08:00
										 |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Cleanup all temp entries upon start.
 | 
					
						
							|  |  |  | 	err = cleanupDir(storageDisk, minioMetaBucket, tmpMetaPrefix) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-02 16:49:46 +08:00
										 |  |  | // Depending on the disk type network or local, initialize storage API.
 | 
					
						
							|  |  |  | func newStorageAPI(disk string) (storage StorageAPI, err error) { | 
					
						
							|  |  |  | 	if !strings.ContainsRune(disk, ':') || filepath.VolumeName(disk) != "" { | 
					
						
							|  |  |  | 		// Initialize filesystem storage API.
 | 
					
						
							|  |  |  | 		return newPosix(disk) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Initialize rpc client storage API.
 | 
					
						
							|  |  |  | 	return newRPCClient(disk) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-31 07:51:59 +08:00
										 |  |  | // House keeping code needed for XL.
 | 
					
						
							|  |  |  | func xlHouseKeeping(storageDisks []StorageAPI) error { | 
					
						
							| 
									
										
										
										
											2016-05-06 06:00:03 +08:00
										 |  |  | 	// This happens for the first time, but keep this here since this
 | 
					
						
							|  |  |  | 	// is the only place where it can be made expensive optimizing all
 | 
					
						
							|  |  |  | 	// other calls. Create minio meta volume, if it doesn't exist yet.
 | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 	var wg = &sync.WaitGroup{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize errs to collect errors inside go-routine.
 | 
					
						
							|  |  |  | 	var errs = make([]error, len(storageDisks)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Initialize all disks in parallel.
 | 
					
						
							|  |  |  | 	for index, disk := range storageDisks { | 
					
						
							| 
									
										
										
										
											2016-06-03 07:34:15 +08:00
										 |  |  | 		if disk == nil { | 
					
						
							|  |  |  | 			errs[index] = errDiskNotFound | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 		wg.Add(1) | 
					
						
							|  |  |  | 		go func(index int, disk StorageAPI) { | 
					
						
							|  |  |  | 			// Indicate this wait group is done.
 | 
					
						
							|  |  |  | 			defer wg.Done() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Attempt to create `.minio`.
 | 
					
						
							|  |  |  | 			err := disk.MakeVol(minioMetaBucket) | 
					
						
							| 
									
										
										
										
											2016-06-09 13:02:10 +08:00
										 |  |  | 			if err != nil && err != errVolumeExists && err != errDiskNotFound && err != errFaultyDisk { | 
					
						
							| 
									
										
										
										
											2016-06-03 07:34:15 +08:00
										 |  |  | 				errs[index] = err | 
					
						
							|  |  |  | 				return | 
					
						
							| 
									
										
										
										
											2016-05-20 17:22:22 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 			// Cleanup all temp entries upon start.
 | 
					
						
							|  |  |  | 			err = cleanupDir(disk, minioMetaBucket, tmpMetaPrefix) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				errs[index] = err | 
					
						
							|  |  |  | 				return | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			errs[index] = nil | 
					
						
							|  |  |  | 		}(index, disk) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Wait for all cleanup to finish.
 | 
					
						
							|  |  |  | 	wg.Wait() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return upon first error.
 | 
					
						
							|  |  |  | 	for _, err := range errs { | 
					
						
							|  |  |  | 		if err == nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							| 
									
										
										
										
											2016-05-20 17:22:22 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 		return toObjectErr(err, minioMetaBucket, tmpMetaPrefix) | 
					
						
							| 
									
										
										
										
											2016-05-06 06:00:03 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-05-25 16:33:39 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Return success here.
 | 
					
						
							| 
									
										
										
										
											2016-05-06 06:00:03 +08:00
										 |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | // Cleanup a directory recursively.
 | 
					
						
							|  |  |  | func cleanupDir(storage StorageAPI, volume, dirPath string) error { | 
					
						
							|  |  |  | 	var delFunc func(string) error | 
					
						
							|  |  |  | 	// Function to delete entries recursively.
 | 
					
						
							|  |  |  | 	delFunc = func(entryPath string) error { | 
					
						
							|  |  |  | 		if !strings.HasSuffix(entryPath, slashSeparator) { | 
					
						
							|  |  |  | 			// No trailing "/" means that this is a file which can be deleted.
 | 
					
						
							|  |  |  | 			return storage.DeleteFile(volume, entryPath) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// If it's a directory, list and call delFunc() for each entry.
 | 
					
						
							|  |  |  | 		entries, err := storage.ListDir(volume, entryPath) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			if err == errFileNotFound { | 
					
						
							|  |  |  | 				// if dirPath prefix never existed.
 | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			return err | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		for _, entry := range entries { | 
					
						
							|  |  |  | 			err = delFunc(pathJoin(entryPath, entry)) | 
					
						
							| 
									
										
										
										
											2016-06-14 16:39:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-06-03 07:34:15 +08:00
										 |  |  | 	err := delFunc(retainSlash(pathJoin(dirPath))) | 
					
						
							|  |  |  | 	return err | 
					
						
							| 
									
										
										
										
											2016-05-09 01:15:34 +08:00
										 |  |  | } |